Changeset View
Changeset View
Standalone View
Standalone View
runners/konsoleprofiles/konsoleprofiles.cpp
- This file was moved from runners/konsolesessions/konsolesessions.cpp.
Show All 12 Lines | |||||
13 | * GNU General Public License for more details | 13 | * GNU General Public License for more details | ||
14 | * | 14 | * | ||
15 | * You should have received a copy of the GNU Library General Public | 15 | * You should have received a copy of the GNU Library General Public | ||
16 | * License along with this program; if not, write to the | 16 | * License along with this program; if not, write to the | ||
17 | * Free Software Foundation, Inc., | 17 | * Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "konsolesessions.h" | 21 | #include "konsoleprofiles.h" | ||
22 | 22 | | |||
23 | // KF | 23 | // KF | ||
24 | #include <KDirWatch> | 24 | #include <KDirWatch> | ||
25 | #include <KToolInvocation> | 25 | #include <KToolInvocation> | ||
26 | #include <KLocalizedString> | 26 | #include <KLocalizedString> | ||
27 | #include <KConfig> | 27 | #include <KConfig> | ||
28 | // Qt | 28 | // Qt | ||
29 | #include <QFileInfo> | 29 | #include <QFileInfo> | ||
30 | #include <QDir> | 30 | #include <QDir> | ||
31 | #include <QStandardPaths> | 31 | #include <QStandardPaths> | ||
32 | 32 | | |||
33 | 33 | | |||
34 | KonsoleSessions::KonsoleSessions(QObject *parent, const QVariantList& args) | 34 | KonsoleProfiles::KonsoleProfiles(QObject *parent, const QVariantList& args) | ||
35 | : Plasma::AbstractRunner(parent, args) | 35 | : Plasma::AbstractRunner(parent, args) | ||
36 | { | 36 | { | ||
37 | setObjectName(QStringLiteral("Konsole Sessions")); | 37 | setObjectName(QStringLiteral("Konsole Profiles")); | ||
38 | 38 | | |||
39 | setIgnoredTypes(Plasma::RunnerContext::File | Plasma::RunnerContext::Directory | Plasma::RunnerContext::NetworkLocation); | 39 | setIgnoredTypes(Plasma::RunnerContext::File | Plasma::RunnerContext::Directory | Plasma::RunnerContext::NetworkLocation); | ||
40 | 40 | | |||
41 | Plasma::RunnerSyntax s(QStringLiteral( ":q:" ), i18n("Finds Konsole sessions matching :q:.")); | 41 | Plasma::RunnerSyntax s(QStringLiteral( ":q:" ), i18n("Finds Konsole profiles matching :q:.")); | ||
42 | s.addExampleQuery(QStringLiteral( "konsole :q:" )); | 42 | s.addExampleQuery(QStringLiteral( "konsole :q:" )); | ||
43 | addSyntax(s); | 43 | addSyntax(s); | ||
44 | 44 | | |||
45 | setDefaultSyntax(Plasma::RunnerSyntax(QStringLiteral( "konsole" ), i18n("Lists all the Konsole sessions in your account."))); | 45 | setDefaultSyntax(Plasma::RunnerSyntax(QStringLiteral( "konsole" ), i18n("Lists all the Konsole profiles in your account."))); | ||
46 | 46 | | |||
47 | connect(this, &Plasma::AbstractRunner::prepare, this, &KonsoleSessions::slotPrepare); | 47 | connect(this, &Plasma::AbstractRunner::prepare, this, &KonsoleProfiles::slotPrepare); | ||
48 | connect(this, &Plasma::AbstractRunner::teardown, this, &KonsoleSessions::slotTeardown); | 48 | connect(this, &Plasma::AbstractRunner::teardown, this, &KonsoleProfiles::slotTeardown); | ||
49 | } | 49 | } | ||
50 | 50 | | |||
51 | KonsoleSessions::~KonsoleSessions() | 51 | KonsoleProfiles::~KonsoleProfiles() | ||
52 | { | 52 | { | ||
53 | } | 53 | } | ||
54 | 54 | | |||
55 | void KonsoleSessions::slotPrepare() | 55 | void KonsoleProfiles::slotPrepare() | ||
56 | { | 56 | { | ||
57 | loadSessions(); | 57 | loadProfiles(); | ||
58 | 58 | | |||
59 | if (!m_sessionWatch) { | 59 | if (!m_profileFilesWatch) { | ||
60 | m_sessionWatch = new KDirWatch(this); | 60 | m_profileFilesWatch = new KDirWatch(this); | ||
61 | const QStringList konsoleDataBaseDirs = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation); | 61 | const QStringList konsoleDataBaseDirs = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation); | ||
62 | for (const QString& konsoleDataBaseDir : konsoleDataBaseDirs) { | 62 | for (const QString& konsoleDataBaseDir : konsoleDataBaseDirs) { | ||
63 | m_sessionWatch->addDir(konsoleDataBaseDir + QLatin1String("/konsole")); | 63 | m_profileFilesWatch->addDir(konsoleDataBaseDir + QLatin1String("/konsole")); | ||
64 | } | 64 | } | ||
65 | 65 | | |||
66 | connect(m_sessionWatch, &KDirWatch::dirty, this, &KonsoleSessions::loadSessions); | 66 | connect(m_profileFilesWatch, &KDirWatch::dirty, this, &KonsoleProfiles::loadProfiles); | ||
67 | connect(m_sessionWatch, &KDirWatch::created, this, &KonsoleSessions::loadSessions); | 67 | connect(m_profileFilesWatch, &KDirWatch::created, this, &KonsoleProfiles::loadProfiles); | ||
68 | connect(m_sessionWatch, &KDirWatch::deleted, this, &KonsoleSessions::loadSessions); | 68 | connect(m_profileFilesWatch, &KDirWatch::deleted, this, &KonsoleProfiles::loadProfiles); | ||
69 | } | 69 | } | ||
70 | } | 70 | } | ||
71 | 71 | | |||
72 | void KonsoleSessions::slotTeardown() | 72 | void KonsoleProfiles::slotTeardown() | ||
73 | { | 73 | { | ||
74 | delete m_sessionWatch; | 74 | delete m_profileFilesWatch; | ||
75 | m_sessionWatch = nullptr; | 75 | m_profileFilesWatch = nullptr; | ||
76 | m_sessions.clear(); | 76 | m_profiles.clear(); | ||
77 | } | 77 | } | ||
78 | 78 | | |||
79 | void KonsoleSessions::loadSessions() | 79 | void KonsoleProfiles::loadProfiles() | ||
80 | { | 80 | { | ||
81 | m_sessions.clear(); | 81 | m_profiles.clear(); | ||
82 | 82 | | |||
83 | QStringList profilesPaths; | 83 | QStringList profilesPaths; | ||
84 | const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("konsole"), QStandardPaths::LocateDirectory); | 84 | const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("konsole"), QStandardPaths::LocateDirectory); | ||
85 | 85 | | |||
86 | for (const auto& dir : dirs) { | 86 | for (const auto& dir : dirs) { | ||
87 | const QStringList fileNames = QDir(dir).entryList({QStringLiteral("*.profile")}); | 87 | const QStringList fileNames = QDir(dir).entryList({QStringLiteral("*.profile")}); | ||
88 | for (const QString& fileName : fileNames) { | 88 | for (const QString& fileName : fileNames) { | ||
89 | profilesPaths.append(dir + QLatin1Char('/') + fileName); | 89 | profilesPaths.append(dir + QLatin1Char('/') + fileName); | ||
90 | } | 90 | } | ||
91 | } | 91 | } | ||
92 | 92 | | |||
93 | for (const auto& profilePath : qAsConst(profilesPaths)) { | 93 | for (const auto& profilePath : qAsConst(profilesPaths)) { | ||
94 | QFileInfo info(profilePath); | 94 | QFileInfo info(profilePath); | ||
95 | const QString profileName = info.baseName(); | 95 | const QString profileName = info.baseName(); | ||
96 | 96 | | |||
97 | KConfig _config(profilePath, KConfig::SimpleConfig); | 97 | KConfig _config(profilePath, KConfig::SimpleConfig); | ||
98 | if (_config.hasGroup("General")) | 98 | if (_config.hasGroup("General")) | ||
99 | { | 99 | { | ||
100 | KonsoleProfileData profileData; | 100 | KonsoleProfileData profileData; | ||
101 | KConfigGroup cfg(&_config, "General"); | 101 | KConfigGroup cfg(&_config, "General"); | ||
102 | profileData.displayName = cfg.readEntry("Name", profileName); | 102 | profileData.displayName = cfg.readEntry("Name", profileName); | ||
103 | profileData.iconName = cfg.readEntry("Icon", QStringLiteral("utilities-terminal")); | 103 | profileData.iconName = cfg.readEntry("Icon", QStringLiteral("utilities-terminal")); | ||
104 | 104 | | |||
105 | m_sessions.insert(profileName, profileData); | 105 | m_profiles.insert(profileName, profileData); | ||
106 | } | 106 | } | ||
107 | } | 107 | } | ||
108 | } | 108 | } | ||
109 | 109 | | |||
110 | void KonsoleSessions::match(Plasma::RunnerContext &context) | 110 | void KonsoleProfiles::match(Plasma::RunnerContext &context) | ||
111 | { | 111 | { | ||
112 | if (m_sessions.isEmpty()) { | 112 | if (m_profiles.isEmpty()) { | ||
113 | return; | 113 | return; | ||
114 | } | 114 | } | ||
115 | 115 | | |||
116 | QString term = context.query(); | 116 | QString term = context.query(); | ||
117 | if (term.length() < 3) { | 117 | if (term.length() < 3) { | ||
118 | return; | 118 | return; | ||
119 | } | 119 | } | ||
120 | 120 | | |||
121 | if (term.compare(QLatin1String( "konsole" ), Qt::CaseInsensitive) == 0) { | 121 | if (term.compare(QLatin1String( "konsole" ), Qt::CaseInsensitive) == 0) { | ||
122 | QHashIterator<QString, KonsoleProfileData> i(m_sessions); | 122 | QHashIterator<QString, KonsoleProfileData> i(m_profiles); | ||
123 | while (i.hasNext()) { | 123 | while (i.hasNext()) { | ||
124 | i.next(); | 124 | i.next(); | ||
125 | const auto& profileData = i.value(); | 125 | const auto& profileData = i.value(); | ||
126 | 126 | | |||
127 | Plasma::QueryMatch match(this); | 127 | Plasma::QueryMatch match(this); | ||
128 | match.setType(Plasma::QueryMatch::PossibleMatch); | 128 | match.setType(Plasma::QueryMatch::PossibleMatch); | ||
129 | match.setRelevance(1.0); | 129 | match.setRelevance(1.0); | ||
130 | match.setIconName(profileData.iconName); | 130 | match.setIconName(profileData.iconName); | ||
131 | match.setData(i.key()); | 131 | match.setData(i.key()); | ||
132 | match.setText(QLatin1String("Konsole: ") + profileData.displayName); | 132 | match.setText(QLatin1String("Konsole: ") + profileData.displayName); | ||
133 | context.addMatch(match); | 133 | context.addMatch(match); | ||
134 | } | 134 | } | ||
135 | } else { | 135 | } else { | ||
136 | if (term.startsWith(QLatin1String("konsole "), Qt::CaseInsensitive)) { | 136 | if (term.startsWith(QLatin1String("konsole "), Qt::CaseInsensitive)) { | ||
137 | term.remove(0, 8); | 137 | term.remove(0, 8); | ||
138 | } | 138 | } | ||
139 | QHashIterator<QString, KonsoleProfileData> i(m_sessions); | 139 | QHashIterator<QString, KonsoleProfileData> i(m_profiles); | ||
140 | while (i.hasNext()) { | 140 | while (i.hasNext()) { | ||
141 | if (!context.isValid()) { | 141 | if (!context.isValid()) { | ||
142 | return; | 142 | return; | ||
143 | } | 143 | } | ||
144 | 144 | | |||
145 | i.next(); | 145 | i.next(); | ||
146 | 146 | | |||
147 | const auto& profileData = i.value(); | 147 | const auto& profileData = i.value(); | ||
Show All 11 Lines | 148 | if (profileData.displayName.contains(term, Qt::CaseInsensitive)) { | |||
159 | } | 159 | } | ||
160 | 160 | | |||
161 | context.addMatch(match); | 161 | context.addMatch(match); | ||
162 | } | 162 | } | ||
163 | } | 163 | } | ||
164 | } | 164 | } | ||
165 | } | 165 | } | ||
166 | 166 | | |||
167 | void KonsoleSessions::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) | 167 | void KonsoleProfiles::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) | ||
168 | { | 168 | { | ||
169 | Q_UNUSED(context) | 169 | Q_UNUSED(context) | ||
170 | const QString session = match.data().toString(); | 170 | const QString profile = match.data().toString(); | ||
171 | 171 | | |||
172 | if (!session.isEmpty()) { | 172 | if (!profile.isEmpty()) { | ||
173 | return; | 173 | return; | ||
174 | } | 174 | } | ||
175 | 175 | | |||
176 | const QStringList args { | 176 | const QStringList args { | ||
177 | QStringLiteral("--profile"), | 177 | QStringLiteral("--profile"), | ||
178 | session | 178 | profile | ||
179 | }; | 179 | }; | ||
180 | KToolInvocation::kdeinitExec(QStringLiteral("konsole"), args); | 180 | KToolInvocation::kdeinitExec(QStringLiteral("konsole"), args); | ||
181 | } | 181 | } | ||
182 | 182 | | |||
183 | 183 | | |||
184 | K_EXPORT_PLASMA_RUNNER(konsolesessions, KonsoleSessions) | 184 | K_EXPORT_PLASMA_RUNNER(konsoleprofiles, KonsoleProfiles) | ||
185 | 185 | | |||
186 | #include "konsolesessions.moc" | 186 | #include "konsoleprofiles.moc" |