core.cpp
1 /*
2  This file is part of KDE Kontact.
3 
4  Copyright (c) 2001 Matthias Hoelzer-Kluepfel <mhk@kde.org>
5  Copyright (c) 2002-2003 Daniel Molkentin <molkentin@kde.org>
6  Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Library General Public
10  License as published by the Free Software Foundation; either
11  version 2 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Library General Public License for more details.
17 
18  You should have received a copy of the GNU Library General Public License
19  along with this library; see the file COPYING.LIB. If not, write to
20  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  Boston, MA 02110-1301, USA.
22 */
23 
24 #include "core.h"
25 
26 #include <tdeparts/part.h>
27 #include <tdeparts/componentfactory.h>
28 #include <kdebug.h>
29 #include <tqtimer.h>
30 #include <tdelocale.h>
31 
32 using namespace Kontact;
33 
34 class Core::Private
35 {
36 public:
37  TQString lastErrorMessage;
38 };
39 
40 Core::Core( TQWidget *parent, const char *name )
41  : KParts::MainWindow( parent, name )
42 {
43  d = new Private;
44  TQTimer* timer = new TQTimer( this );
45  mLastDate = TQDate::currentDate();
46  connect(timer, TQ_SIGNAL( timeout() ), TQ_SLOT( checkNewDay() ) );
47  timer->start( 1000*60 );
48 }
49 
50 Core::~Core()
51 {
52  delete d;
53 }
54 
55 KParts::ReadOnlyPart *Core::createPart( const char *libname )
56 {
57  kdDebug(5601) << "Core::createPart(): " << libname << endl;
58 
59  TQMap<TQCString,KParts::ReadOnlyPart *>::ConstIterator it;
60  it = mParts.find( libname );
61  if ( it != mParts.end() ) return it.data();
62 
63  kdDebug(5601) << "Creating new KPart" << endl;
64 
65  int error = 0;
66  KParts::ReadOnlyPart *part =
67  KParts::ComponentFactory::
68  createPartInstanceFromLibrary<KParts::ReadOnlyPart>
69  ( libname, this, 0, this, "kontact", TQStringList(), &error );
70 
71  KParts::ReadOnlyPart *pimPart = dynamic_cast<KParts::ReadOnlyPart*>( part );
72  if ( pimPart ) {
73  mParts.insert( libname, pimPart );
74  TQObject::connect( pimPart, TQ_SIGNAL( destroyed( TQObject * ) ),
75  TQ_SLOT( slotPartDestroyed( TQObject * ) ) );
76  } else {
77  // TODO move to KParts::ComponentFactory
78  switch( error ) {
79  case KParts::ComponentFactory::ErrNoServiceFound:
80  d->lastErrorMessage = i18n( "No service found" );
81  break;
82  case KParts::ComponentFactory::ErrServiceProvidesNoLibrary:
83  d->lastErrorMessage = i18n( "Program error: the .desktop file for the service does not have a Library key." );
84  break;
85  case KParts::ComponentFactory::ErrNoLibrary:
86  d->lastErrorMessage = KLibLoader::self()->lastErrorMessage();
87  break;
88  case KParts::ComponentFactory::ErrNoFactory:
89  d->lastErrorMessage = i18n( "Program error: the library %1 does not provide a factory." ).arg( libname );
90  break;
91  case KParts::ComponentFactory::ErrNoComponent:
92  d->lastErrorMessage = i18n( "Program error: the library %1 does not support creating components of the specified type" ).arg( libname );
93  break;
94  }
95  kdWarning(5601) << d->lastErrorMessage << endl;
96  }
97 
98  return pimPart;
99 }
100 
101 void Core::slotPartDestroyed( TQObject * obj )
102 {
103  // the part was deleted, we need to remove it from the part map to not return
104  // a dangling pointer in createPart
105  TQMap<TQCString, KParts::ReadOnlyPart*>::Iterator end = mParts.end();
106  TQMap<TQCString, KParts::ReadOnlyPart*>::Iterator it = mParts.begin();
107  for ( ; it != end; ++it ) {
108  if ( it.data() == obj ) {
109  mParts.remove( it );
110  return;
111  }
112  }
113 }
114 
115 void Core::checkNewDay()
116 {
117  if ( mLastDate != TQDate::currentDate() )
118  emit dayChanged( TQDate::currentDate() );
119 
120  mLastDate = TQDate::currentDate();
121 }
122 
123 TQString Core::lastErrorMessage() const
124 {
125  return d->lastErrorMessage;
126 }
127 
128 #include "core.moc"
void dayChanged(const TQDate &)
Emitted when a new day starts.