Changeset View
Changeset View
Standalone View
Standalone View
kcms/componentchooser/componentchooserterminal.cpp
Show All 28 Lines | |||||
29 | #include <kurlrequester.h> | 29 | #include <kurlrequester.h> | ||
30 | #include <kconfiggroup.h> | 30 | #include <kconfiggroup.h> | ||
31 | #include <KLocalizedString> | 31 | #include <KLocalizedString> | ||
32 | #include <KServiceTypeTrader> | 32 | #include <KServiceTypeTrader> | ||
33 | 33 | | |||
34 | #include <QUrl> | 34 | #include <QUrl> | ||
35 | 35 | | |||
36 | CfgTerminalEmulator::CfgTerminalEmulator(QWidget *parent) | 36 | CfgTerminalEmulator::CfgTerminalEmulator(QWidget *parent) | ||
37 | : QWidget(parent), Ui::TerminalEmulatorConfig_UI(), CfgPlugin() | 37 | : CfgPlugin(parent) | ||
38 | { | 38 | { | ||
39 | setupUi(this); | 39 | connect(this, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated), this, &CfgTerminalEmulator::selectTerminalEmulator); | ||
40 | connect(terminalCombo, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated), this, &CfgTerminalEmulator::selectTerminalEmulator); | | |||
41 | } | 40 | } | ||
42 | 41 | | |||
43 | CfgTerminalEmulator::~CfgTerminalEmulator() { | 42 | CfgTerminalEmulator::~CfgTerminalEmulator() { | ||
44 | } | 43 | } | ||
45 | 44 | | |||
46 | void CfgTerminalEmulator::selectTerminalEmulator(int index) | 45 | void CfgTerminalEmulator::selectTerminalEmulator(int index) | ||
47 | { | 46 | { | ||
48 | if (index == terminalCombo->count() - 1) { | 47 | if (index == count() - 1) { | ||
49 | selectTerminalApp(); | 48 | selectTerminalApp(); | ||
50 | } else { | 49 | } else { | ||
51 | emit changed(m_currentIndex != index); | 50 | emit changed(m_currentIndex != index); | ||
52 | } | 51 | } | ||
53 | } | 52 | } | ||
54 | 53 | | |||
55 | void CfgTerminalEmulator::defaults() | | |||
56 | { | | |||
57 | if (m_konsoleIndex != -1) { | | |||
58 | terminalCombo->setCurrentIndex(m_konsoleIndex); | | |||
59 | } | | |||
60 | } | | |||
61 | | ||||
62 | bool CfgTerminalEmulator::isDefaults() const | | |||
63 | { | | |||
64 | return m_konsoleIndex == -1 || m_konsoleIndex == terminalCombo->currentIndex(); | | |||
65 | } | | |||
66 | | ||||
67 | void CfgTerminalEmulator::load(KConfig *) | 54 | void CfgTerminalEmulator::load(KConfig *) | ||
68 | { | 55 | { | ||
69 | TerminalSettings settings; | 56 | TerminalSettings settings; | ||
70 | const QString terminal = settings.terminalApplication(); | 57 | const QString terminal = settings.terminalApplication(); | ||
71 | 58 | | |||
59 | clear(); | ||||
72 | m_currentIndex = -1; | 60 | m_currentIndex = -1; | ||
73 | terminalCombo->clear(); | 61 | m_defaultIndex = -1; | ||
74 | 62 | | |||
75 | const auto constraint = QStringLiteral("'TerminalEmulator' in Categories AND (not exist NoDisplay OR NoDisplay == false)"); | 63 | const auto constraint = QStringLiteral("'TerminalEmulator' in Categories AND (not exist NoDisplay OR NoDisplay == false)"); | ||
76 | const auto terminalEmulators = KServiceTypeTrader::self()->query(QStringLiteral("Application"), constraint); | 64 | const auto terminalEmulators = KServiceTypeTrader::self()->query(QStringLiteral("Application"), constraint); | ||
77 | for (const auto &service : terminalEmulators) { | 65 | for (const auto &service : terminalEmulators) { | ||
78 | terminalCombo->addItem(QIcon::fromTheme(service->icon()), service->name(), service->exec()); | 66 | addItem(QIcon::fromTheme(service->icon()), service->name(), service->exec()); | ||
79 | 67 | | |||
80 | if (!terminal.isEmpty() && service->exec() == terminal) { | 68 | if (!terminal.isEmpty() && service->exec() == terminal) { | ||
81 | terminalCombo->setCurrentIndex(terminalCombo->count() - 1); | 69 | setCurrentIndex(count() - 1); | ||
82 | m_currentIndex = terminalCombo->count() - 1; | 70 | m_currentIndex = count() - 1; | ||
83 | } | 71 | } | ||
84 | if (service->exec() == QStringLiteral("konsole")) { | 72 | if (service->exec() == QStringLiteral("konsole")) { | ||
85 | m_konsoleIndex = terminalCombo->count() - 1; | 73 | m_defaultIndex = count() - 1; | ||
86 | } | 74 | } | ||
87 | } | 75 | } | ||
88 | 76 | | |||
89 | if (!terminal.isEmpty() && m_currentIndex == -1) { | 77 | if (!terminal.isEmpty() && m_currentIndex == -1) { | ||
90 | // we have a terminal specified by the user | 78 | // we have a terminal specified by the user | ||
91 | terminalCombo->addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), terminal, terminal); | 79 | addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), terminal, terminal); | ||
92 | terminalCombo->setCurrentIndex(terminalCombo->count() - 1); | 80 | setCurrentIndex(count() - 1); | ||
93 | m_currentIndex = terminalCombo->count() - 1; | 81 | m_currentIndex = count() - 1; | ||
94 | } | 82 | } | ||
95 | 83 | | |||
96 | // add a other option to add a new terminal emulator with KOpenWithDialog | 84 | // add a other option to add a new terminal emulator with KOpenWithDialog | ||
97 | terminalCombo->addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), i18n("Other..."), QStringLiteral()); | 85 | addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), i18n("Other..."), QStringLiteral()); | ||
98 | 86 | | |||
99 | emit changed(false); | 87 | emit changed(false); | ||
100 | } | 88 | } | ||
101 | 89 | | |||
102 | void CfgTerminalEmulator::save(KConfig *) | 90 | void CfgTerminalEmulator::save(KConfig *) | ||
103 | { | 91 | { | ||
104 | const QString terminal = terminalCombo->currentData().toString(); | 92 | const QString terminal = currentData().toString(); | ||
105 | m_currentIndex = terminalCombo->currentIndex(); | 93 | m_currentIndex = currentIndex(); | ||
106 | 94 | | |||
107 | TerminalSettings settings; | 95 | TerminalSettings settings; | ||
108 | settings.setTerminalApplication(terminal); | 96 | settings.setTerminalApplication(terminal); | ||
109 | settings.save(); | 97 | settings.save(); | ||
110 | 98 | | |||
99 | m_currentIndex = currentIndex(); | ||||
100 | | ||||
111 | QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.klauncher5"), | 101 | QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.klauncher5"), | ||
112 | QStringLiteral("/KLauncher"), | 102 | QStringLiteral("/KLauncher"), | ||
113 | QStringLiteral("org.kde.KLauncher"), | 103 | QStringLiteral("org.kde.KLauncher"), | ||
114 | QStringLiteral("reparseConfiguration")); | 104 | QStringLiteral("reparseConfiguration")); | ||
115 | QDBusConnection::sessionBus().send(message); | 105 | QDBusConnection::sessionBus().send(message); | ||
116 | emit changed(false); | 106 | emit changed(false); | ||
117 | } | 107 | } | ||
118 | 108 | | |||
119 | void CfgTerminalEmulator::selectTerminalApp() | 109 | void CfgTerminalEmulator::selectTerminalApp() | ||
120 | { | 110 | { | ||
121 | QList<QUrl> urlList; | 111 | QList<QUrl> urlList; | ||
122 | KOpenWithDialog dlg(urlList, i18n("Select preferred terminal application:"), QString(), this); | 112 | KOpenWithDialog dlg(urlList, i18n("Select preferred terminal application:"), QString(), this); | ||
123 | // hide "Run in &terminal" here, we don't need it for a Terminal Application | 113 | // hide "Run in &terminal" here, we don't need it for a Terminal Application | ||
124 | dlg.hideRunInTerminal(); | 114 | dlg.hideRunInTerminal(); | ||
125 | dlg.setSaveNewApplications(true); | 115 | dlg.setSaveNewApplications(true); | ||
126 | if (dlg.exec() != QDialog::Accepted) { | 116 | if (dlg.exec() != QDialog::Accepted) { | ||
127 | terminalCombo->setCurrentIndex(m_currentIndex); | 117 | setCurrentIndex(m_currentIndex); | ||
128 | return; | 118 | return; | ||
129 | } | 119 | } | ||
130 | const auto service = dlg.service(); | 120 | const auto service = dlg.service(); | ||
131 | 121 | | |||
132 | // if the selected service is already in the list | 122 | // if the selected service is already in the list | ||
133 | const auto matching = terminalCombo->model()->match(terminalCombo->model()->index(0,0), Qt::DisplayRole, service->exec()); | 123 | const auto matching = model()->match(model()->index(0,0), Qt::DisplayRole, service->exec()); | ||
134 | if (!matching.isEmpty()) { | 124 | if (!matching.isEmpty()) { | ||
135 | const int index = matching.at(0).row(); | 125 | const int index = matching.at(0).row(); | ||
136 | terminalCombo->setCurrentIndex(index); | 126 | setCurrentIndex(index); | ||
137 | changed(index != m_currentIndex); | 127 | changed(index != m_currentIndex); | ||
138 | } else { | 128 | } else { | ||
139 | const QString icon = !service->icon().isEmpty() ? service->icon() : QStringLiteral("application-x-shellscript"); | 129 | const QString icon = !service->icon().isEmpty() ? service->icon() : QStringLiteral("application-x-shellscript"); | ||
140 | terminalCombo->insertItem(terminalCombo->count() -1, QIcon::fromTheme(icon), service->name(), service->exec()); | 130 | insertItem(count() -1, QIcon::fromTheme(icon), service->name(), service->exec()); | ||
141 | terminalCombo->setCurrentIndex(terminalCombo->count() - 2); | 131 | setCurrentIndex(count() - 2); | ||
142 | 132 | | |||
143 | changed(true); | 133 | changed(true); | ||
144 | } | 134 | } | ||
145 | 135 | | |||
146 | } | 136 | } | ||
147 | // vim: sw=4 ts=4 noet | 137 | // vim: sw=4 ts=4 noet |