Backport fix google translator
[kdepim.git] / akonadiconsole / instanceselector.cpp
blob1b863cc099a50e9da0d80dd713dc2193ab5d9b87
1 /*
2 This file is part of Akonadi.
4 Copyright (c) 2012 Volker Krause <vkrause@kde.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 USA.
22 #include "instanceselector.h"
23 #include "ui_instanceselector.h"
25 #include "akonadiconsole_debug.h"
26 #include <QIcon>
27 #include <KLocalizedString>
28 #include <akonadi/private/instance_p.h>
29 #include <akonadi/private/dbus_p.h>
31 #include <QApplication>
32 #include <QDBusConnection>
33 #include <QDBusConnectionInterface>
34 #include <QStandardItemModel>
35 #include <KConfigGroup>
36 #include <QDialogButtonBox>
37 #include <QPushButton>
38 #include <QVBoxLayout>
40 InstanceSelector::InstanceSelector(const QString &remoteHost, QWidget *parent, Qt::WindowFlags flags)
41 : QDialog(parent, flags),
42 ui(new Ui::InstanceSelector),
43 m_remoteHost(remoteHost),
44 mWindow(Q_NULLPTR)
46 QWidget *mainWidget = new QWidget(this);
47 QVBoxLayout *mainLayout = new QVBoxLayout;
48 setLayout(mainLayout);
49 mainLayout->addWidget(mainWidget);
50 ui->setupUi(mainWidget);
52 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close);
53 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
54 okButton->setDefault(true);
55 okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
56 connect(buttonBox, &QDialogButtonBox::accepted, this, &InstanceSelector::slotAccept);
57 connect(buttonBox, &QDialogButtonBox::rejected, this, &InstanceSelector::slotReject);
58 mainLayout->addWidget(buttonBox);
59 okButton->setIcon(QIcon::fromTheme(QStringLiteral("network-connect")));
60 okButton->setText(QStringLiteral("Connect"));
62 const QStringList insts = instances();
63 qCDebug(AKONADICONSOLE_LOG) << "Found running Akonadi instances:" << insts;
64 if (insts.size() <= 1) {
65 m_instance = QString::fromUtf8(qgetenv("AKONADI_INSTANCE"));
66 if (insts.size() == 1 && m_instance.isEmpty()) {
67 m_instance = insts.first();
69 slotAccept();
70 } else {
71 QStandardItemModel *model = new QStandardItemModel(this);
72 foreach (const QString &inst, insts) {
73 QStandardItem *item = new QStandardItem;
74 item->setText(inst.isEmpty() ? QStringLiteral("<global>") : inst);
75 item->setData(inst, Qt::UserRole);
76 model->appendRow(item);
78 connect(ui->listView, &QAbstractItemView::doubleClicked, this, &QDialog::accept);
79 ui->listView->setModel(model);
80 show();
84 InstanceSelector::~InstanceSelector()
86 delete mWindow;
89 void InstanceSelector::slotAccept()
91 if (ui->listView->model()) { // there was something to select
92 const QModelIndexList selection = ui->listView->selectionModel()->selectedRows();
93 if (selection.size() != 1) {
94 return;
96 m_instance = selection.first().data(Qt::UserRole).toString();
98 QDialog::accept();
100 qputenv("AKONADI_INSTANCE", m_instance.toUtf8());
101 MainWindow *mWindow = new MainWindow;
102 if (!m_remoteHost.isEmpty()) {
103 mWindow->setWindowTitle(QStringLiteral("Remote Akonadi Console (%1)").arg(m_remoteHost));
104 } else if (!m_instance.isEmpty()) {
105 mWindow->setWindowTitle(QStringLiteral("Akonadi Console (Instance: %1)").arg(m_instance));
107 mWindow->show();
110 void InstanceSelector::slotReject()
112 QDialog::reject();
113 QApplication::quit();
116 QStringList InstanceSelector::instances()
118 const QString currentInstance = Akonadi::Instance::identifier();
119 Akonadi::Instance::setIdentifier(QString());
120 const QString lockService = Akonadi::DBus::serviceName(Akonadi::DBus::ControlLock);
121 Akonadi::Instance::setIdentifier(currentInstance);
123 const QStringList allServices = QDBusConnection::sessionBus().interface()->registeredServiceNames();
124 QStringList insts;
125 foreach (const QString &service, allServices) {
126 if (!service.startsWith(lockService)) {
127 continue;
129 insts.push_back(service.mid(lockService.length() + 1));
131 return insts;