• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kopete/kopete

  • sources
  • kde-4.12
  • kdenetwork
  • kopete
  • kopete
kopeteapplication.cpp
Go to the documentation of this file.
1 /*
2  kopete.cpp
3 
4  Kopete Instant Messenger Main Class
5 
6  Copyright (c) 2001-2002 by Duncan Mac-Vicar Prett <duncan@kde.org>
7  Copyright (c) 2002-2003 by Martijn Klingens <klingens@kde.org>
8 
9  Kopete (c) 2001-2003 by the Kopete developers <kopete-devel@kde.org>
10 
11  *************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  *************************************************************************
19 */
20 
21 #include "kopeteapplication.h"
22 
23 #include <qtimer.h>
24 #include <qregexp.h>
25 
26 #include <kconfig.h>
27 #include <kdebug.h>
28 #include <kglobal.h>
29 #include <klocale.h>
30 #include <kcmdlineargs.h>
31 #include <kmessagebox.h>
32 #include <solid/networking.h>
33 
34 #include "addaccountwizard.h"
35 #include "kabcpersistence.h"
36 #include "kopeteaccount.h"
37 #include "kopeteaccountmanager.h"
38 #include "kopetestatusmanager.h"
39 #include "kopetestatusitems.h"
40 #include "kopetebehaviorsettings.h"
41 #include "kopetecommandhandler.h"
42 #include "kopetecontactlist.h"
43 #include "kopeteglobal.h"
44 #include "kopetefileengine.h"
45 #include "kopetemimetypehandler.h"
46 #include "kopetepluginmanager.h"
47 #include "kopeteprotocol.h"
48 #include "kopetestdaction.h"
49 #include "kopeteuiglobal.h"
50 #include "kopetewindow.h"
51 #include "kopeteviewmanager.h"
52 #include "kopeteidentitymanager.h"
53 #include "kopetedbusinterface.h"
54 
55 KopeteApplication::KopeteApplication()
56 : KUniqueApplication( true, true )
57 {
58  setQuitOnLastWindowClosed( false );
59  m_isShuttingDown = false;
60 
61  //Create the identity manager
62  Kopete::IdentityManager::self()->load();
63 
64  m_mainWindow = new KopeteWindow( 0 );
65 
66  Kopete::PluginManager::self();
67 
68  Kopete::UI::Global::setMainWidget( m_mainWindow );
69 
70  /*
71  * FIXME: This is a workaround for a quite odd problem:
72  * When starting up kopete and the msn plugin gets loaded it can bring up
73  * a messagebox, in case the msg configuration is missing. This messagebox
74  * will result in a QApplication::enter_loop() call, an event loop is
75  * created. At this point however the loop_level is 0, because this is all
76  * still inside the KopeteApplication constructor, before the exec() call from main.
77  * When the messagebox is finished the loop_level will drop down to zero and
78  * QApplication thinks the application shuts down (this is usually the case
79  * when the loop_level goes down to zero) . So it emits aboutToQuit(), to
80  * which KApplication is connected and re-emits shutdown() , to which again
81  * KXmlGuiWindow (a KopeteWindow instance exists already) is connected. KXmlGuiWindow's
82  * shuttingDown() slot calls queryExit() which results in KopeteWindow::queryExit()
83  * calling unloadPlugins() . This of course is wrong and just shouldn't happen.
84  * The workaround is to simply delay the initialization of all this to a point
85  * where the loop_level is already > 0 . That is why I moved all the code from
86  * the constructor to the initialize() method and added this single-shot-timer
87  * setup. (Simon)
88  *
89  * Additionally, it makes the GUI appear less 'blocking' during startup, so
90  * there is a secondary benefit as well here. (Martijn)
91  */
92  QTimer::singleShot( 0, this, SLOT(slotLoadPlugins()) );
93 
94  m_fileEngineHandler = new Kopete::FileEngineHandler();
95 
96  //Create the emoticon installer
97  m_emoticonHandler = new Kopete::EmoticonMimeTypeHandler;
98 
99  //Create the DBus interface for org.kde.kopete
100  new KopeteDBusInterface(this);
101 }
102 
103 KopeteApplication::~KopeteApplication()
104 {
105  kDebug( 14000 ) ;
106 
107  if ( ! m_isShuttingDown )
108  {
109  // destruct was called without proper shutdown, dbus quit maybe?
110  m_isShuttingDown = true;
111 
112  // close all windows
113  QList<KMainWindow*> members = KMainWindow::memberList();
114  QList<KMainWindow*>::iterator it, itEnd = members.end();
115  for ( it = members.begin(); it != itEnd; ++it )
116  (*it)->close();
117 
118  // shutdown plugin manager
119  Kopete::PluginManager::self()->shutdown();
120 
121  // destroy all plugins until KopeteApplication is alive
122  Kopete::PluginList list = Kopete::PluginManager::self()->loadedPlugins();
123  foreach ( Kopete::Plugin *plugin, list )
124  delete plugin;
125  }
126 
127  delete m_fileEngineHandler;
128  delete m_emoticonHandler;
129  //kDebug( 14000 ) << "Done";
130 }
131 
132 void KopeteApplication::slotLoadPlugins()
133 {
134  // we have to load the address book early, because calling this enters the Qt event loop when there are remote resources.
135  // The plugin manager is written with the assumption that Kopete will not reenter the event loop during plugin load,
136  // otherwise lots of things break as plugins are loaded, then contacts are added to incompletely initialised MCLVIs
137  Kopete::KABCPersistence::self()->addressBook();
138 
139  //Create the command handler (looks silly)
140  Kopete::CommandHandler::commandHandler();
141 
142  //Create the view manager
143  KopeteViewManager::viewManager();
144 
145  // the account manager should be created after the identity manager is created
146  Kopete::AccountManager::self()->load();
147  Kopete::ContactList::self()->load();
148 
149  KSharedConfig::Ptr config = KGlobal::config();
150 
151  // Parse command-line arguments
152  KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
153 
154  bool showConfigDialog = false;
155 
156  KConfigGroup pluginsGroup = config->group( "Plugins" );
157 
158  /* FIXME: This is crap, if something purged that groups but your accounts
159  * are still working kopete will load the necessary plugins but still show the
160  * stupid accounts dialog (of course empty at that time because account data
161  * gets loaded later on). [mETz - 29.05.2004]
162  */
163  if ( !pluginsGroup.exists() )
164  showConfigDialog = true;
165 
166  // Listen to arguments
167  /*
168  // TODO: conflicts with emoticon installer and the general meaning
169  // of %U in kopete.desktop
170  if ( args->count() > 0 )
171  {
172  showConfigDialog = false;
173  for ( int i = 0; i < args->count(); i++ )
174  Kopete::PluginManager::self()->setPluginEnabled( args->arg( i ), true );
175  }
176  */
177 
178  // Prevent plugins from loading? (--disable=foo,bar)
179  foreach ( const QString &disableArg, args->getOption( "disable" ).split( ',' ))
180  {
181  showConfigDialog = false;
182  Kopete::PluginManager::self()->setPluginEnabled( disableArg, false );
183  }
184 
185  // Load some plugins exclusively? (--load-plugins=foo,bar)
186  if ( args->isSet( "load-plugins" ) )
187  {
188  pluginsGroup.deleteGroup( KConfigBase::Global );
189  showConfigDialog = false;
190  foreach ( const QString &plugin, args->getOption( "load-plugins" ).split( ',' ))
191  Kopete::PluginManager::self()->setPluginEnabled( plugin, true );
192  }
193 
194  config->sync();
195 
196  // Disable plugins altogether? (--noplugins)
197  if ( !args->isSet( "plugins" ) )
198  {
199  // If anybody reenables this I'll get a sword and make a nice chop-suy out
200  // of your body :P [mETz - 29.05.2004]
201  // This screws up kopeterc because there is no way to get the Plugins group back!
202  //config->deleteGroup( "Plugins", true );
203 
204  showConfigDialog = false;
205  // pretend all plugins were loaded :)
206  QTimer::singleShot(0, this, SLOT(slotAllPluginsLoaded()));
207  }
208  else
209  {
210  Kopete::PluginManager::self()->loadAllPlugins();
211  }
212 
213  connect( Kopete::PluginManager::self(), SIGNAL(allPluginsLoaded()),
214  this, SLOT(slotAllPluginsLoaded()));
215 
216  if( showConfigDialog )
217  {
218  // No plugins specified. Show the config dialog.
219  // FIXME: Although it's a bit stupid it is theoretically possible that a user
220  // explicitly configured Kopete to not load plugins on startup. In this
221  // case we don't want this dialog. We need some other config setting
222  // like a bool hasRunKopeteBefore or so to trigger the loading of the
223  // wizard. Maybe using the last run version number is more useful even
224  // as it also allows for other features. - Martijn
225  // FIXME: Possibly we need to influence the showConfigDialog bool based on the
226  // command line arguments processed below. But how exactly? - Martijn
227  // NB: the command line args are completely broken atm.
228  // I don't want to fix them for 3.5 as plugin loading will change for KDE4. - Will
229  AddAccountWizard *m_addwizard = new AddAccountWizard( Kopete::UI::Global::mainWidget(), true );
230  m_addwizard->exec();
231  Kopete::AccountManager::self()->save();
232  }
233 }
234 
235 
236 void KopeteApplication::slotAllPluginsLoaded()
237 {
238  KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
239 
240  //FIXME: this should probably ask for the identities to connect instead of all accounts
241  // --noconnect not specified?
242 
243  Kopete::OnlineStatusManager::Category initStatus = Kopete::OnlineStatusManager::self()->initialStatus();
244  Kopete::OnlineStatusManager::Category setStatus = Kopete::OnlineStatusManager::Offline;
245 
246  if ( args->isSet( "connect" ) && initStatus != Kopete::OnlineStatusManager::Offline &&
247  ( Solid::Networking::status() == Solid::Networking::Unknown ||
248  Solid::Networking::status() == Solid::Networking::Connected ) ){
249 
250  setStatus = initStatus;
251 
252  }
253 
254  QList <Kopete::Status::StatusItem *> statusList = Kopete::StatusManager::self()->getRootGroup()->childList();
255  QString message, title;
256  bool found = false;
257 
258  //find first Status for OnlineStatus
259  for ( QList <Kopete::Status::StatusItem *>::ConstIterator it = statusList.constBegin(); it != statusList.constEnd(); ++it ) {
260  if ( ! (*it)->isGroup() && (*it)->category() == setStatus ) {
261  title = (*it)->title();
262  message = (static_cast <Kopete::Status::Status*> (*it))->message(); //if it is not group, it status
263  found = true;
264  break;
265  }
266  }
267 
268  if ( found )
269  {
270 
271  if ( setStatus != Kopete::OnlineStatusManager::Offline )
272  {
273  Kopete::AccountManager::self()->setOnlineStatus(initStatus, Kopete::StatusMessage(title, message), Kopete::AccountManager::ConnectIfOffline);
274  }
275 
276  Kopete::StatusManager::self()->setGlobalStatus(setStatus, Kopete::StatusMessage(title, message));
277 
278  } else {
279 
280  if ( setStatus != Kopete::OnlineStatusManager::Offline )
281  {
282  Kopete::AccountManager::self()->setOnlineStatus(initStatus, QString(), Kopete::AccountManager::ConnectIfOffline);
283  }
284 
285  }
286 
287  kDebug(14000)<< "initial status set in config: " << initStatus;
288 
289  QStringList connectArgs = args->getOptionList( "autoconnect" );
290 
291  // toConnect will contain all the protocols to connect to
292  QStringList toConnect;
293 
294  for ( QStringList::ConstIterator i = connectArgs.constBegin(); i != connectArgs.constEnd(); ++i )
295  {
296  foreach ( const QString &connectArg, (*i).split(','))
297  toConnect.append( connectArg );
298  }
299 
300  for ( QStringList::ConstIterator i = toConnect.constBegin(); i != toConnect.constEnd(); ++i )
301  {
302  QRegExp rx( QLatin1String( "([^\\|]*)\\|\\|(.*)" ) );
303  rx.indexIn( *i );
304  QString protocolId = rx.cap( 1 );
305  QString accountId = rx.cap( 2 );
306 
307  if ( accountId.isEmpty() )
308  {
309  if ( protocolId.isEmpty() )
310  accountId = *i;
311  else
312  continue;
313  }
314 
315  QListIterator<Kopete::Account *> it( Kopete::AccountManager::self()->accounts() );
316  Kopete::Account *account;
317  while ( it.hasNext() )
318  {
319  account = it.next();
320  if ( ( account->accountId() == accountId ) )
321  {
322  if ( protocolId.isEmpty() || account->protocol()->pluginId() == protocolId )
323  {
324  account->connect();
325  break;
326  }
327  }
328  }
329  }
330 
331  // Parse any passed URLs/files
332  handleURLArgs();
333 }
334 
335 
336 
337 int KopeteApplication::newInstance()
338 {
339 // kDebug(14000) ;
340  handleURLArgs();
341 
342  return KUniqueApplication::newInstance();
343 }
344 
345 void KopeteApplication::handleURLArgs()
346 {
347  KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
348 // kDebug(14000) << "called with " << args->count() << " arguments to handle.";
349 
350  if ( args->count() > 0 )
351  {
352  for ( int i = 0; i < args->count(); i++ )
353  {
354  KUrl u( args->url( i ) );
355  if ( !u.isValid() )
356  continue;
357 
358  Kopete::MimeTypeHandler::dispatchURL( u );
359  } // END for()
360  } // END args->count() > 0
361 }
362 
363 
364 void KopeteApplication::quitKopete()
365 {
366  kDebug( 14000 ) ;
367 
368  m_isShuttingDown = true;
369 
370  // close all windows
371  QList<KMainWindow*> members = KMainWindow::memberList();
372  QList<KMainWindow*>::iterator it, itEnd = members.end();
373  for ( it = members.begin(); it != itEnd; ++it)
374  {
375  if ( !(*it)->close() )
376  {
377  m_isShuttingDown = false;
378  break;
379  }
380  }
381 
382  if ( m_isShuttingDown && m_mainWindow )
383  {
384  m_mainWindow->deleteLater();
385  m_mainWindow = 0;
386  }
387 }
388 
389 
390 void KopeteApplication::commitData( QSessionManager &sm )
391 {
392  m_isShuttingDown = true;
393  KUniqueApplication::commitData( sm );
394 }
395 
396 #include "kopeteapplication.moc"
397 // vim: set noet ts=4 sts=4 sw=4:
kopeteapplication.h
kopetewindow.h
addaccountwizard.h
KopeteApplication::KopeteApplication
KopeteApplication()
Definition: kopeteapplication.cpp:55
accountId
QString accountId
Definition: kopete-account-kconf_update.cpp:31
KopeteApplication::slotLoadPlugins
void slotLoadPlugins()
Load all plugins.
Definition: kopeteapplication.cpp:132
KopeteApplication::newInstance
virtual int newInstance()
Definition: kopeteapplication.cpp:337
KopeteDBusInterface
Public D-Bus interface for Kopete.
Definition: kopetedbusinterface.h:30
KUniqueApplication
KopeteApplication::quitKopete
void quitKopete()
Quit Kopete, closing all the windows, which causes application shutdown This method marks Kopete as '...
Definition: kopeteapplication.cpp:364
AddAccountWizard
Definition: addaccountwizard.h:43
KopeteWindow
Definition: kopetewindow.h:48
KopeteApplication::commitData
virtual void commitData(QSessionManager &sm)
Definition: kopeteapplication.cpp:390
KopeteApplication::~KopeteApplication
~KopeteApplication()
Definition: kopeteapplication.cpp:103
kopetedbusinterface.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kopete/kopete

Skip menu "kopete/kopete"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal