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

Nepomuk-Core

  • KTp
telepathy-handler-application.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2011 Daniele E. Domenichelli <daniele.domenichelli@gmail.com>
3 * Copyright (C) 1999 Preston Brown <pbrown@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 #include "telepathy-handler-application.h"
21 #include "debug.h"
22 
23 #include <cstdlib>
24 
25 #include <QTimer>
26 #include <KCmdLineArgs>
27 #include <KDebug>
28 
29 #include <TelepathyQt/Types>
30 #include <TelepathyQt/Debug>
31 
32 
33 extern bool kde_kdebug_enable_dbus_interface;
34 
35 namespace KTp
36 {
37 
38 class TelepathyHandlerApplication::Private
39 {
40 public:
41  Private(TelepathyHandlerApplication *q);
42  ~Private();
43 
44  void _k_onInitialTimeout();
45  void _k_onTimeout();
46 
47  static KComponentData initHack();
48  void init(int initialTimeout, int timeout);
49 
50  TelepathyHandlerApplication *q;
51 
52  static bool s_persist;
53  static bool s_debug;
54 
55  int initialTimeout;
56  int timeout;
57  QTimer *timer;
58  bool firstJobStarted;
59  QAtomicInt jobCount;
60 };
61 
62 TelepathyHandlerApplication::Private::Private(TelepathyHandlerApplication *q)
63  : q(q),
64  firstJobStarted(false),
65  jobCount(0)
66 {
67 }
68 
69 TelepathyHandlerApplication::Private::~Private()
70 {
71 }
72 
73 void TelepathyHandlerApplication::Private::_k_onInitialTimeout()
74 {
75  if (jobCount == 0 && jobCount.fetchAndAddOrdered(-1) == 0) {
76  // m_jobCount is now -1
77  kDebug() << "No job received. Exiting";
78  QCoreApplication::quit();
79  }
80 }
81 
82 void TelepathyHandlerApplication::Private::_k_onTimeout()
83 {
84  if (jobCount == 0 && jobCount.fetchAndAddOrdered(-1) == 0) {
85  // m_jobCount is now -1
86  kDebug() << "Timeout. Exiting";
87  QCoreApplication::quit();
88  }
89 }
90 
91 // this gets called before even entering QApplication::QApplication()
92 KComponentData TelepathyHandlerApplication::Private::initHack()
93 {
94  // This is a very very very ugly hack that attempts to solve the following problem:
95  // D-Bus service activated applications inherit the environment of dbus-daemon.
96  // Normally, in KDE, startkde sets these environment variables. However, the session's
97  // dbus-daemon is started before this happens, which means that dbus-daemon does NOT
98  // have these variables in its environment and therefore all service-activated UIs
99  // think that they are not running in KDE. This causes Qt not to load the KDE platform
100  // plugin, which leaves the UI in a sorry state, using a completely wrong theme,
101  // wrong colors, etc...
102  // See also:
103  // - https://bugs.kde.org/show_bug.cgi?id=269861
104  // - https://bugs.kde.org/show_bug.cgi?id=267770
105  // - https://git.reviewboard.kde.org/r/102194/
106  // Here we are just going to assume that kde-telepathy is always used in KDE and
107  // not anywhere else. This is probably the best that we can do.
108  setenv("KDE_FULL_SESSION", "true", 0);
109  setenv("KDE_SESSION_VERSION", "4", 0);
110 
111  KComponentData cData(KCmdLineArgs::aboutData());
112  KCmdLineOptions handler_options;
113  handler_options.add("persist", ki18n("Persistent mode (do not exit on timeout)"));
114  handler_options.add("debug", ki18n("Show Telepathy debugging information"));
115  KCmdLineArgs::addCmdLineOptions(handler_options, ki18n("KDE Telepathy"), "kde-telepathy", "kde");
116  KCmdLineArgs *args = KCmdLineArgs::parsedArgs("kde-telepathy");
117  Private::s_persist = args->isSet("persist");
118  Private::s_debug = args->isSet("debug");
119 
120  return cData;
121 }
122 
123 void TelepathyHandlerApplication::Private::init(int initialTimeout, int timeout)
124 {
125  this->initialTimeout = initialTimeout;
126  this->timeout = timeout;
127 
128  // If timeout < 0 we let the application exit when the last window is closed,
129  // Otherwise we handle it with the timeout
130  if (timeout >= 0) {
131  q->setQuitOnLastWindowClosed(false);
132  }
133 
134  // Register TpQt4 types
135  Tp::registerTypes();
136 
137  // Install TpQt4 debug callback
138  KTp::Debug::installCallback(s_debug);
139 
140  // Enable KDebug DBus interface
141  // FIXME This must be enabled here because there is a bug in plasma
142  // it should be removed when this is fixed
143  kde_kdebug_enable_dbus_interface = s_debug;
144 
145  if (!Private::s_persist) {
146  timer = new QTimer(q);
147  if (initialTimeout >= 0) {
148  q->connect(timer, SIGNAL(timeout()), q, SLOT(_k_onInitialTimeout()));
149  timer->start(initialTimeout);
150  }
151  }
152 }
153 
154 bool TelepathyHandlerApplication::Private::s_persist = false;
155 bool TelepathyHandlerApplication::Private::s_debug = false;
156 
157 
158 TelepathyHandlerApplication::TelepathyHandlerApplication(bool GUIenabled,
159  int initialTimeout,
160  int timeout)
161  : KApplication(GUIenabled, Private::initHack()),
162  d(new Private(this))
163 {
164  d->init(initialTimeout, timeout);
165 }
166 
167 TelepathyHandlerApplication::TelepathyHandlerApplication(Display *display,
168  Qt::HANDLE visual,
169  Qt::HANDLE colormap,
170  int initialTimeout,
171  int timeout)
172  : KApplication(display, visual, colormap, Private::initHack()),
173  d(new Private(this))
174 {
175  d->init(initialTimeout, timeout);
176 }
177 
178 TelepathyHandlerApplication::~TelepathyHandlerApplication()
179 {
180  delete d;
181 }
182 
183 int TelepathyHandlerApplication::newJob()
184 {
185  TelepathyHandlerApplication *app = qobject_cast<TelepathyHandlerApplication*>(KApplication::kApplication());
186  TelepathyHandlerApplication::Private *d = app->d;
187 
188  int ret = d->jobCount.fetchAndAddOrdered(1);
189  if (!Private::s_persist) {
190  if (d->timer->isActive()) {
191  d->timer->stop();
192  }
193  if (!d->firstJobStarted) {
194  if (d->initialTimeout) {
195  disconnect(d->timer, SIGNAL(timeout()), app, SLOT(_k_onInitialTimeout()));
196  }
197  if (d->timeout >= 0) {
198  connect(d->timer, SIGNAL(timeout()), app, SLOT(_k_onTimeout()));
199  }
200  d->firstJobStarted = true;
201  }
202  }
203  kDebug() << "New job started." << d->jobCount << "jobs currently running";
204  return ret;
205 }
206 
207 void TelepathyHandlerApplication::jobFinished()
208 {
209  TelepathyHandlerApplication *app = qobject_cast<TelepathyHandlerApplication*>(KApplication::kApplication());
210  TelepathyHandlerApplication::Private *d = app->d;
211 
212  if (d->jobCount.fetchAndAddOrdered(-1) <= 1) {
213  if (!Private::s_persist && d->timeout >= 0) {
214  kDebug() << "No other jobs at the moment. Starting timer.";
215  d->timer->start(d->timeout);
216  }
217  }
218  kDebug() << "Job finished." << d->jobCount << "jobs currently running";
219 }
220 
221 } // namespace KTp
222 
223 #include "telepathy-handler-application.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Fri Mar 22 2013 10:58:52 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ktp-common-internals API Reference

Skip menu "ktp-common-internals API Reference"
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