delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / kuiserver / uiserver.cpp
blob30459cf86bc908d386aa14abd5099c9b7b56dca3
1 /**
2 * This file is part of the KDE project
3 * Copyright (C) 2006-2008 Rafael Fernández López <ereslibre@kde.org>
4 * Copyright (C) 2001 George Staikos <staikos@kde.org>
5 * Copyright (C) 2000 Matej Koss <koss@miesto.sk>
6 * David Faure <faure@kde.org>
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 version 2 as published by the Free Software Foundation.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 #include "uiserver.h"
24 #include "uiserver_p.h"
25 #include "jobviewadaptor.h"
26 #include "jobviewserveradaptor.h"
27 #include "progresslistmodel.h"
28 #include "progresslistdelegate.h"
30 #include <QtGui/QWidget>
31 #include <QtGui/QAction>
32 #include <QtGui/QBoxLayout>
33 #include <QtGui/QCloseEvent>
35 #include <ksqueezedtextlabel.h>
36 #include <kconfig.h>
37 #include <kconfigdialog.h>
38 #include <kstandarddirs.h>
39 #include <kuniqueapplication.h>
40 #include <kaboutdata.h>
41 #include <kcmdlineargs.h>
42 #include <kglobal.h>
43 #include <klocale.h>
44 #include <kpushbutton.h>
45 #include <ktabwidget.h>
46 #include <kstatusbar.h>
47 #include <kdebug.h>
48 #include <kdialog.h>
49 #include <ksystemtrayicon.h>
50 #include <kmenu.h>
51 #include <kaction.h>
52 #include <klineedit.h>
53 #include <kio/jobclasses.h>
54 #include <kjob.h>
56 UIServer *UIServer::s_uiserver = 0;
58 UIServer::JobView::JobView(QObject *parent)
59 : QObject(parent)
61 m_objectPath.setPath(QString("/JobViewServer/JobView_%1").arg(s_jobId));
63 new JobViewAdaptor(this);
64 QDBusConnection::sessionBus().registerObject(m_objectPath.path(), this);
67 void UIServer::JobView::terminate(const QString &errorMessage)
69 QModelIndex index = s_uiserver->m_progressListModel->indexForJob(this);
71 s_uiserver->m_progressListModel->setData(index, JobInfo::Cancelled, ProgressListModel::State);
73 if (errorMessage.isNull())
75 s_uiserver->m_progressListFinishedModel->newJob(s_uiserver->m_progressListModel->data(index, ProgressListModel::ApplicationName).toString(),
76 s_uiserver->m_progressListModel->data(index, ProgressListModel::Icon).toString(),
77 s_uiserver->m_progressListModel->data(index, ProgressListModel::Capabilities).toInt());
80 s_uiserver->m_progressListModel->finishJob(this);
82 QDBusConnection::sessionBus().unregisterObject(m_objectPath.path(), QDBusConnection::UnregisterTree);
85 void UIServer::JobView::setSuspended(bool suspended)
87 QModelIndex index = s_uiserver->m_progressListModel->indexForJob(this);
89 s_uiserver->m_progressListModel->setData(index, suspended ? JobInfo::Suspended
90 : JobInfo::Running, ProgressListModel::State);
93 void UIServer::JobView::setTotalAmount(qlonglong amount, const QString &unit)
95 QModelIndex index = s_uiserver->m_progressListModel->indexForJob(this);
97 if (unit == "bytes") {
98 s_uiserver->m_progressListModel->setData(index, amount ? KGlobal::locale()->formatByteSize(amount)
99 : QString(), ProgressListModel::SizeTotals);
100 } else if (unit == "files") {
101 s_uiserver->m_progressListModel->setData(index, amount ? i18np("%1 file", "%1 files", amount)
102 : QString(), ProgressListModel::SizeTotals);
103 } else if (unit == "dirs") {
104 s_uiserver->m_progressListModel->setData(index, amount ? i18np("%1 folder", "%1 folders", amount)
105 : QString(), ProgressListModel::SizeTotals);
109 void UIServer::JobView::setProcessedAmount(qlonglong amount, const QString &unit)
111 QModelIndex index = s_uiserver->m_progressListModel->indexForJob(this);
113 if (unit == "bytes") {
114 s_uiserver->m_progressListModel->setData(index, amount ? KGlobal::locale()->formatByteSize(amount)
115 : QString(), ProgressListModel::SizeProcessed);
116 } else if (unit == "files") {
117 s_uiserver->m_progressListModel->setData(index, amount ? i18np("%1 file", "%1 files", amount)
118 : QString(), ProgressListModel::SizeProcessed);
119 } else if (unit == "dirs") {
120 s_uiserver->m_progressListModel->setData(index, amount ? i18np("%1 folder", "%1 folders", amount)
121 : QString(), ProgressListModel::SizeProcessed);
125 void UIServer::JobView::setPercent(uint percent)
127 QModelIndex index = s_uiserver->m_progressListModel->indexForJob(this);
129 if (index.isValid())
130 s_uiserver->m_progressListModel->setData(index, percent, ProgressListModel::Percent);
133 void UIServer::JobView::setSpeed(qlonglong bytesPerSecond)
135 QModelIndex index = s_uiserver->m_progressListModel->indexForJob(this);
137 if (index.isValid())
138 s_uiserver->m_progressListModel->setData(index, bytesPerSecond ? KGlobal::locale()->formatByteSize(bytesPerSecond)
139 : QString(), ProgressListModel::Speed);
142 void UIServer::JobView::setInfoMessage(const QString &infoMessage)
144 QModelIndex index = s_uiserver->m_progressListModel->indexForJob(this);
146 if (index.isValid())
147 s_uiserver->m_progressListModel->setData(index, infoMessage, ProgressListModel::Message);
150 bool UIServer::JobView::setDescriptionField(uint number, const QString &name, const QString &value)
152 QModelIndex index = s_uiserver->m_progressListModel->indexForJob(this);
154 if (index.isValid())
155 return s_uiserver->m_progressListModel->setDescriptionField(index, number, name, value);
157 return false;
160 void UIServer::JobView::clearDescriptionField(uint number)
162 QModelIndex index = s_uiserver->m_progressListModel->indexForJob(this);
164 if (index.isValid())
165 s_uiserver->m_progressListModel->clearDescriptionField(index, number);
168 QDBusObjectPath UIServer::JobView::objectPath() const
170 return m_objectPath;
173 UIServer::UIServer()
174 : KXmlGuiWindow(0)
176 // Register necessary services and D-Bus adaptors.
177 new JobViewServerAdaptor(this);
178 QDBusConnection::sessionBus().registerService(QLatin1String("org.kde.JobViewServer"));
179 QDBusConnection::sessionBus().registerObject(QLatin1String("/JobViewServer"), this);
181 QWidget *centralWidget = new QWidget(this);
182 QVBoxLayout *layout = new QVBoxLayout;
183 centralWidget->setLayout(layout);
185 tabWidget = new KTabWidget(this);
187 QString configure = i18n("Configure...");
189 toolBar = addToolBar(configure);
190 toolBar->setMovable(false);
191 toolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
192 QAction *configureAction = toolBar->addAction(configure);
193 configureAction->setIcon(KIcon("configure"));
194 configureAction->setIconText(configure);
196 connect(configureAction, SIGNAL(triggered(bool)), this,
197 SLOT(showConfigurationDialog()));
199 connect(QDBusConnection::sessionBus().interface(), SIGNAL(serviceOwnerChanged(const QString&,const QString&,const QString&)), this,
200 SLOT(slotServiceOwnerChanged(const QString&,const QString&,const QString&)));
203 toolBar->addSeparator();
205 searchText = new KLineEdit(toolBar);
206 searchText->setClickMessage(i18n("Search"));
207 searchText->setClearButtonShown(true);
209 toolBar->addWidget(searchText);
211 listProgress = new QListView(tabWidget);
212 listProgress->setAlternatingRowColors(true);
213 listProgress->setObjectName("progresslist");
214 listProgress->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
216 listFinished = new QListView(tabWidget);
217 listFinished->setAlternatingRowColors(true);
218 listFinished->setVisible(false);
219 listFinished->setObjectName("progresslistFinished");
220 listFinished->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
222 layout->addWidget(listProgress);
224 tabWidget->addTab(centralWidget, i18n("In Progress"));
225 //tabWidget->addTab(listFinished, i18n("Finished"));
227 setCentralWidget(tabWidget);
229 m_progressListModel = new ProgressListModel(this);
230 m_progressListFinishedModel = new ProgressListModel(this);
232 listProgress->setModel(m_progressListModel);
233 listFinished->setModel(m_progressListFinishedModel);
235 progressListDelegate = new ProgressListDelegate(this, listProgress);
236 progressListDelegate->setSeparatorPixels(5);
237 progressListDelegate->setLeftMargin(10);
238 progressListDelegate->setRightMargin(10);
239 progressListDelegate->setMinimumItemHeight(100);
240 progressListDelegate->setMinimumContentWidth(300);
241 progressListDelegate->setEditorHeight(20);
242 listProgress->setItemDelegate(progressListDelegate);
244 progressListDelegateFinished = new ProgressListDelegate(this, listFinished);
245 progressListDelegateFinished->setSeparatorPixels(5);
246 progressListDelegateFinished->setLeftMargin(10);
247 progressListDelegateFinished->setRightMargin(10);
248 progressListDelegateFinished->setMinimumItemHeight(100);
249 progressListDelegateFinished->setMinimumContentWidth(300);
250 progressListDelegateFinished->setEditorHeight(20);
251 listFinished->setItemDelegate(progressListDelegateFinished);
253 applySettings();
256 UIServer::~UIServer()
260 UIServer* UIServer::createInstance()
262 s_uiserver = new UIServer;
263 return s_uiserver;
266 QDBusObjectPath UIServer::requestView(const QString &appName, const QString &appIconName, int capabilities)
268 if (isHidden()) show();
270 s_jobId++;
272 // Since s_jobId is an unsigned int, if we got overflow and go back to 0,
273 // be sure we do not assign 0 to a valid job, since 0 is reserved only for
274 // reporting problems.
275 if (!s_jobId) s_jobId++;
277 JobView *jobView = m_progressListModel->newJob(appName, appIconName, capabilities);
279 return jobView->objectPath();
282 void UIServer::updateConfiguration()
284 Configuration::self()->writeConfig();
287 void UIServer::applySettings()
289 KSystemTrayIcon *m_systemTray = new KSystemTrayIcon(this);
290 m_systemTray->setIcon(KSystemTrayIcon::loadIcon("display"));
291 m_systemTray->show();
294 void UIServer::closeEvent(QCloseEvent *event)
296 event->ignore();
297 hide();
300 void UIServer::showConfigurationDialog()
302 if (KConfigDialog::showDialog("configuration"))
303 return;
305 KConfigDialog *dialog = new KConfigDialog(this, "configuration",
306 Configuration::self());
308 UIConfigurationDialog *configurationUI = new UIConfigurationDialog(0);
310 dialog->addPage(configurationUI, i18n("Behavior"), "display");
312 connect(dialog, SIGNAL(settingsChanged(const QString&)), this,
313 SLOT(updateConfiguration()));
314 dialog->enableButton(KDialog::Help, false);
315 dialog->show();
318 void UIServer::slotServiceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner)
320 kDebug() << "dbus name: " << name << " oldowner: " << oldOwner << " newowner: " << newOwner;
324 /// ===========================================================
327 UIConfigurationDialog::UIConfigurationDialog(QWidget *parent)
328 : QWidget(parent)
330 setupUi(this);
331 adjustSize();
334 UIConfigurationDialog::~UIConfigurationDialog()
339 /// ===========================================================
342 uint UIServer::s_jobId = 0;
344 extern "C" KDE_EXPORT int kdemain(int argc, char **argv)
346 // GS 5/2001 - I changed the name to "KDE" to make it look better
347 // in the titles of dialogs which are displayed.
348 KAboutData aboutdata("kuiserver", "kdelibs4", ki18n("Progress Manager"),
349 "0.8", ki18n("KDE Progress Information UI Server"),
350 KAboutData::License_GPL, ki18n("(C) 2000-2008, KDE Team"));
351 aboutdata.addAuthor(ki18n("Rafael Fernández López"),ki18n("Maintainer"),"ereslibre@kde.org");
352 aboutdata.addAuthor(ki18n("David Faure"),ki18n("Former maintainer"),"faure@kde.org");
353 aboutdata.addAuthor(ki18n("Matej Koss"),ki18n("Developer"),"koss@miesto.sk");
355 KCmdLineArgs::init( argc, argv, &aboutdata );
356 // KCmdLineArgs::addCmdLineOptions( options );
357 KUniqueApplication::addCmdLineOptions();
359 if (!KUniqueApplication::start())
361 kDebug(7024) << "kuiserver is already running!";
362 return (0);
365 KUniqueApplication app;
367 // This app is started automatically, no need for session management
368 app.disableSessionManagement();
369 app.setQuitOnLastWindowClosed( false );
370 //app.dcopClient()->setDaemonMode( true );
372 UIServer::createInstance();
374 return app.exec();
377 #include "uiserver.moc"
378 #include "uiserver_p.moc"