File: controly.cpp

package info (click to toggle)
launchy 2.5-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,536 kB
  • ctags: 1,561
  • sloc: cpp: 11,735; sh: 162; makefile: 45
file content (327 lines) | stat: -rw-r--r-- 8,105 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
Launchy: Application Launcher
Copyright (C) 2007-2009  Josh Karlin

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "precompiled.h"
#include "controly.h"
#include "ControlPanelItemFinder.h"

controlyPlugin* gControlyInstance = NULL;

controlyPlugin::controlyPlugin() {
	HASH_controly = qHash(QString("controly"));
	gui = NULL;
}

controlyPlugin::~controlyPlugin() {
}


void controlyPlugin::setPath(QString * path)
{
	libPath = *path;
}


void controlyPlugin::init() {
	if (gControlyInstance == NULL) {
		// init() is currently called multiple times:
		// on plugin init and every time when opening the options

		gControlyInstance = this;

		// get config / settings directory (base for 'temporary' icon cache dir)
		QString iniFilename = (*settings)->fileName();
		QFileInfo info(iniFilename);
		QString userDataPath = info.absolutePath();
		
		// create (and store) controly icon cache folder
		iconCreator.setImagePath(userDataPath + "/controly-icon-cache");
		iconCreator.setBaseImageFileName("controly-icon-");
		iconCreator.setDefaultImageFileName(getIcon());
		iconCreator.setForceDefaultImage(false);
	}
}

void controlyPlugin::getID(uint* id)
{
	*id = HASH_controly;
}

void controlyPlugin::getName(QString* str)
{
	*str = "Controly";
}

QString controlyPlugin::getIcon()
{
	return getIconPath() + "controly.png";
}

QString controlyPlugin::getIconPath() const
{
	return "/usr/share/launchy/plugins/icons/";
}


#ifdef Q_WS_WIN
void controlyPlugin::getApps(QList<CatItem>* items) {
	ControlPanelItemFinder *pCplFinder = new ControlPanelItemFinder(HASH_controly, &iconCreator, items);
	pCplFinder->findItems();

	if (pCplFinder) {
		delete pCplFinder;
		pCplFinder = NULL;
	}
}
#endif


void controlyPlugin::getCatalog(QList<CatItem>* items)
{
	getApps(items);

	CatItem tmp = CatItem("Launchy.controly", "Launchy", HASH_controly, getIcon());
	tmp.usage = 5000;
	items->push_back(tmp);
}


bool controlyPlugin::isMatch(QString text1, QString text2)
{
	int text2Length = text2.count();
	int curChar = 0;

	foreach(QChar c, text1)
	{
		if (c.toLower() == text2[curChar].toLower())
		{
			++curChar;
			if (curChar >= text2Length)
			{
				return true;
			}
		}
	}

	return false;
}


void controlyPlugin::addCatItem(QString text, QList<CatItem>* results, QString fullName, QString shortName)
{
	if (text.length() == 0 || isMatch(shortName, text))
	{
		CatItem& item = CatItem(fullName, shortName, HASH_controly, getIconPath() + fullName.toLower() + ".png");
		item.usage = (*settings)->value("controly/" + shortName.replace(" ", "") , 0).toInt();
		results->push_back(item);
	}
}


void controlyPlugin::updateUsage(CatItem& item)
{
	(*settings)->setValue("controly/" + item.shortName.replace(" ", ""), item.usage + 1);
}


void controlyPlugin::getResults(QList<InputData>* inputData, QList<CatItem>* results)
{
	// if user enters "*.controly", dynamically return all elements that we added to the primary catalog (for informational / debugging purposes only)
	if (inputData->size() == 1) {
		const QString & text = inputData->first().getText();

		if (text.compare("*.controly") == 0) {
			QList<CatItem> controlyCatalog;
			getCatalog(&controlyCatalog);
			CatItem temp("ItemCount.controly", QString::number(controlyCatalog.size()), HASH_controly, getIcon());
			temp.usage = 32000;
			results->append(temp);
			(*results) += controlyCatalog;
		}
	}

	if (inputData->count() != 2)
		return;

	if (inputData->first().getTopResult().id == HASH_controly) {
		QString text = inputData->at(1).getText();
		addCatItem(text, results, "Launchy.options", "Options");
		addCatItem(text, results, "Launchy.rebuild", "Rebuild Catalog");
		addCatItem(text, results, "Launchy.exit", "Exit");
	}	
}


int controlyPlugin::launchItem(QList<InputData>* inputData, CatItem* item)
{
	item = item; // Compiler warning

	if (inputData->count() == 1) {
		// no parameters, just the item itsef

		QString path = item->fullPath;

		if (path.contains(",@")) {
			// dll cpl item indexing containing items, e.g. 'main.cpl,@1'

			runProgram("control.exe", item->fullPath); //runProgram(cmd, args);
			// use toNativeSeparators()?
		} else if ((path.startsWith("csidl:", Qt::CaseInsensitive)) && (path.endsWith(".controly", Qt::CaseSensitive))) {
			// Constant special item ID list (CSIDL)

			// shell instance object (special shell extension folder), e.g. 'csidl:0x0014.controly' ('shellinstance:0x0014')
			QString folderId = path.mid(strlen("csidl:"), strlen(path.toAscii())-strlen("csidl:")-strlen(".controly")); // e.g. 0x0014 = CSIDL_FONTS;
			bool ok;
			int folderIdx = folderId.toLong(&ok, 16);
			if (ok) {
				LPITEMIDLIST pidl;
				HRESULT hres = SHGetFolderLocation(NULL, folderIdx, NULL, 0, &pidl);
				if (hres == S_OK) {
					SHELLEXECUTEINFO sei;
					memset(&sei, 0, sizeof(sei));
					sei.cbSize = sizeof(SHELLEXECUTEINFO);
					sei.fMask = SEE_MASK_IDLIST;
					sei.hwnd = NULL;
					sei.lpVerb = NULL;
					sei.lpFile = NULL;
					sei.lpParameters = NULL;
					sei.lpDirectory = NULL;
					sei.nShow = SW_SHOW;
					sei.hInstApp = NULL;
					sei.lpIDList = pidl;
					sei.hProcess = NULL;
					//it seems we don't need CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
					ShellExecuteEx(&sei);

					CoTaskMemFree((void*)pidl); // needs objbase.h/ole32.lib
				}
			}
		} else {
			// exe cpl item with command line parameters, e.g. 'rundll32.exe shell32.dll,Options_RunDLL 1'
			// or item defined via application name, e.g. 'control.exe /name Microsoft.WindowsUpdate'

			QStringList spl = path.split(".exe ");
			if (spl.size() == 2) {
				// split size currently is always 2, as assured above
				QString executable = spl[0] + ".exe";
				QString parameters = spl[1];
				runProgram(executable, parameters);
			} else {
				runProgram(path, "");
			}
		}

		return 1;
	}

	if (inputData->count() != 2)
		return 1;

	CatItem last = inputData->last().getTopResult();
	if (last.shortName == "Options")
	{
		updateUsage(last);
		return MSG_CONTROL_OPTIONS;
	}
	else if (last.shortName == "Rebuild Catalog")
	{
		updateUsage(last);
		return MSG_CONTROL_REBUILD;
	}
	else if (last.shortName == "Exit")
	{
		updateUsage(last);
		return MSG_CONTROL_EXIT;
	}
	return 1;

}

#ifdef WITH_GUI
void controlyPlugin::doDialog(QWidget* parent, QWidget** newDlg) {
	if (gui != NULL) {
		return;
	}
	gui = new Gui(parent);
	*newDlg = gui;
}

void controlyPlugin::endDialog(bool accept) {
	if (accept) {
		gui->writeOptions();
		init();
	}
	if (gui != NULL) {
		delete gui;
	}
	gui = NULL;
}
#endif

int controlyPlugin::msg(int msgId, void* wParam, void* lParam)
{
	int handled = 0;
	switch (msgId)
	{		
		case MSG_INIT:
			init();
			handled = 1;
			break;
		case MSG_GET_ID:
			getID((uint*) wParam);
			handled = 1;
			break;
		case MSG_GET_NAME:
			getName((QString*) wParam);
			handled = 1;
			break;
		case MSG_GET_CATALOG:
			getCatalog((QList<CatItem>*) wParam);
			handled = 1;
			break;
		case MSG_GET_RESULTS:
			getResults((QList<InputData>*) wParam, (QList<CatItem>*) lParam);
			handled = 1;
			break;
		case MSG_LAUNCH_ITEM:
			handled = launchItem((QList<InputData>*) wParam, (CatItem*) lParam);
			break;

		case MSG_HAS_DIALOG:
			handled = true;
			break;
		case MSG_DO_DIALOG:
			doDialog((QWidget*) wParam, (QWidget**) lParam);
			break;
		case MSG_END_DIALOG:
			endDialog(wParam != 0);
			break;

		case MSG_PATH:
			setPath((QString *) wParam);
			break;

		default:
			break;
	}
		
	return handled;
}

Q_EXPORT_PLUGIN2(controly, controlyPlugin)