forked from humdingerb/quicklaunch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickLaunch.cpp
More file actions
440 lines (366 loc) · 8.72 KB
/
QuickLaunch.cpp
File metadata and controls
440 lines (366 loc) · 8.72 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* Copyright 2010-2018. All rights reserved.
* Distributed under the terms of the MIT license.
*
* Author:
* Humdinger, humdingerb@gmail.com
*
* A graphical launch panel finding an app via a query.
*/
#include "DeskButton.h"
#include "QLFilter.h"
#include "QuickLaunch.h"
#include <Deskbar.h>
#include <Catalog.h>
#include <storage/NodeMonitor.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Application"
const char* kApplicationSignature = "application/x-vnd.humdinger-quicklaunch";
extern "C" _EXPORT BView* instantiate_deskbar_item();
extern "C" _EXPORT BView*
instantiate_deskbar_item()
{
return new DeskButton();
}
QLApp::QLApp()
:
BApplication(kApplicationSignature)
{
if (fSettings.GetDeskbar()) // make sure the replicant is shown
_AddToDeskbar();
fSetupWindow = new SetupWindow(fSettings.GetSetupWindowFrame());
fMainWindow = new MainWindow();
}
QLApp::~QLApp()
{
stop_watching(this);
if (fSettings.Lock()) {
fSettings.SetSearchTerm(fMainWindow->GetSearchString());
fSettings.SaveSettings();
fSettings.Unlock();
}
BMessenger messengerMain(fMainWindow);
if (messengerMain.IsValid() && messengerMain.LockTarget())
fMainWindow->Quit();
BMessenger messengerSetup(fSetupWindow);
if (messengerSetup.IsValid() && messengerSetup.LockTarget())
fSetupWindow->Quit();
}
#pragma mark -- BApplication Overrides --
void
QLApp::AboutRequested()
{
BString text = B_TRANSLATE_COMMENT(
"QuickLaunch %version%\n"
"\twritten by Humdinger\n"
"\tCopyright %years%\n\n"
"QuickLaunch quickly starts any installed application. "
"Just enter the first few letters of its name and choose "
"from a list of all found programs.\n",
"Don't change the variables %years% and %version%.");
text.ReplaceAll("%version%", kVersion);
text.ReplaceAll("%years%", kCopyright);
BAlert* alert = new BAlert("about", text.String(),
B_TRANSLATE("Thank you"));
BTextView* view = alert->TextView();
BFont font;
view->SetStylable(true);
view->GetFont(&font);
font.SetSize(font.Size()+4);
font.SetFace(B_BOLD_FACE);
view->SetFontAndColor(0, 11, &font);
alert->Go();
}
void
QLApp::MessageReceived(BMessage* message)
{
switch (message->what) {
case SETUP_BUTTON:
{
if (fSetupWindow->IsHidden()) {
SetWindowsFeel(0);
fSetupWindow->Show();
} else
fSetupWindow->Hide();
break;
}
case HELP_BUTTON:
{
app_info info;
BPath path;
be_roster->GetActiveAppInfo(&info);
BEntry entry(&info.ref);
entry.GetPath(&path);
path.GetParent(&path);
path.Append("ReadMe.html");
entry = path.Path();
entry_ref ref;
entry.GetRef(&ref);
be_roster->Launch(&ref);
break;
}
case DESKBAR_CHK:
{
int32 value;
message->FindInt32("be:value", &value);
if (fSettings.Lock()) {
fSettings.SetDeskbar(value);
fSettings.Unlock();
}
if (value)
_AddToDeskbar();
else
_RemoveFromDeskbar();
break;
}
case VERSION_CHK:
{
int32 value;
message->FindInt32("be:value", &value);
if (fSettings.Lock()) {
fSettings.SetShowVersion(value);
fSettings.Unlock();
}
if (!fMainWindow->fListView->IsEmpty()) {
fMainWindow->LockLooper();
fMainWindow->fListView->Invalidate();
fMainWindow->UnlockLooper();
}
break;
}
case PATH_CHK:
{
int32 value;
message->FindInt32("be:value", &value);
if (fSettings.Lock()) {
fSettings.SetShowPath(value);
fSettings.Unlock();
}
if (!fMainWindow->fListView->IsEmpty()) {
fMainWindow->LockLooper();
fMainWindow->fListView->Invalidate();
fMainWindow->UnlockLooper();
}
break;
}
case SEARCHSTART_CHK:
{
int32 value;
message->FindInt32("be:value", &value);
if (fSettings.Lock()) {
fSettings.SetSearchStart(value);
fSettings.Unlock();
}
if (!fMainWindow->fListView->IsEmpty()) {
fMainWindow->LockLooper();
fMainWindow->BuildList();
fMainWindow->UnlockLooper();
}
break;
}
case DELAY_CHK:
{
int32 value;
message->FindInt32("be:value", &value);
if (fSettings.Lock()) {
fSettings.SetDelay(value);
fSettings.Unlock();
}
fMainWindow->LockLooper();
fMainWindow->PostMessage('fltr');
fMainWindow->UnlockLooper();
break;
}
case SAVESEARCH_CHK:
{
int32 value;
message->FindInt32("be:value", &value);
if (fSettings.Lock()) {
fSettings.SetSaveSearch(value);
fSettings.Unlock();
}
break;
}
case SINGLECLICK_CHK:
{
int32 value;
message->FindInt32("be:value", &value);
if (fSettings.Lock()) {
fSettings.SetSingleClick(value);
fSettings.Unlock();
}
break;
}
case ONTOP_CHK:
{
int32 value;
message->FindInt32("be:value", &value);
if (fSettings.Lock()) {
fSettings.SetOnTop(value);
fSettings.Unlock();
}
break;
}
case IGNORE_CHK:
{
if (fSettings.fIgnoreList->IsEmpty()) {
fSetupWindow->LockLooper();
fSetupWindow->fChkIgnore->SetValue(false);
if (fSettings.Lock()) {
fSettings.SetShowIgnore(false);
fSettings.Unlock();
}
fSetupWindow->UnlockLooper();
break;
}
int32 value;
message->FindInt32("be:value", &value);
if (fSettings.Lock()) {
fSettings.SetShowIgnore(value);
fSettings.Unlock();
}
if (!fSettings.fIgnoreList->IsEmpty()) {
fSetupWindow->fChkIgnore->SetValue(value);
fMainWindow->fListView->LockLooper();
fMainWindow->BuildList();
fMainWindow->fListView->UnlockLooper();
}
break;
}
case FILEPANEL:
{
if (!fSettings.fIgnoreList->IsEmpty()) {
fSetupWindow->LockLooper();
fSetupWindow->fChkIgnore->SetValue(true);
if (fSettings.Lock()) {
fSettings.SetShowIgnore(true);
fSettings.Unlock();
}
fSetupWindow->UnlockLooper();
} else {
fSetupWindow->LockLooper();
fSetupWindow->fChkIgnore->SetValue(false);
if (fSettings.Lock()) {
fSettings.SetShowIgnore(false);
fSettings.Unlock();
}
fSetupWindow->UnlockLooper();
}
fMainWindow->fListView->LockLooper();
int32 selection = fMainWindow->fListView->CurrentSelection();
float position = fMainWindow->GetScrollPosition();
fMainWindow->BuildList();
fMainWindow->fListView->Select((selection
< fMainWindow->fListView->CountItems())
? selection : fMainWindow->fListView->CountItems() - 1);
fMainWindow->SetScrollPosition(position);
fMainWindow->fListView->UnlockLooper();
break;
}
case B_NODE_MONITOR:
{
int32 opcode = message->GetInt32("opcode", -1);
if ((opcode == B_DEVICE_MOUNTED)
|| (opcode == B_DEVICE_UNMOUNTED)) {
fMainWindow->fListView->LockLooper();
float position = fMainWindow->GetScrollPosition();
fMainWindow->BuildList();
fMainWindow->SetScrollPosition(position);
fMainWindow->fListView->UnlockLooper();
}
break;
}
default:
{
BApplication::MessageReceived(message);
break;
}
}
}
bool
QLApp::QuitRequested()
{
return true;
}
void
QLApp::ReadyToRun()
{
BRect frame = fSettings.GetMainWindowFrame();
fMainWindow->MoveTo(frame.LeftTop());
fMainWindow->ResizeTo(frame.right - frame.left, 0);
fMainWindow->Show();
fSettings.InitLists();
fMainWindow->fListView->LockLooper();
fMainWindow->BuildList();
fMainWindow->fListView->Select(0);
fMainWindow->fListView->UnlockLooper();
fSetupWindow->Hide();
fSetupWindow->Show();
if (fSettings.GetSaveSearch()) {
BMessenger messenger(fMainWindow);
BMessage message(NEW_FILTER);
messenger.SendMessage(&message);
}
watch_node(NULL, B_WATCH_MOUNT, this);
}
#pragma mark -- Public Methods --
void
QLApp::SetWindowsFeel(int32 value)
{
fMainWindow->LockLooper();
fMainWindow->SetFeel(value
? B_MODAL_ALL_WINDOW_FEEL : B_NORMAL_WINDOW_FEEL);
fMainWindow->UnlockLooper();
}
#pragma mark -- Private Methods --
status_t
our_image(image_info& image)
{
int32 cookie = 0;
while (get_next_image_info(B_CURRENT_TEAM, &cookie, &image) == B_OK) {
if ((char *)our_image >= (char *)image.text
&& (char *)our_image <= (char *)image.text + image.text_size)
return B_OK;
}
return B_ERROR;
}
void
QLApp::_AddToDeskbar()
{
BDeskbar deskbar;
if (deskbar.IsRunning() && !deskbar.HasItem("QuickLaunch")) {
image_info info;
entry_ref ref;
if (our_image(info) == B_OK
&& get_ref_for_path(info.name, &ref) == B_OK) {
int32 id;
status_t err = deskbar.AddItem(&ref, &id);
if (err != B_OK) {
printf("info_name: %s, ref_name: %s, id: %" B_PRId32
"\nerr: %" B_PRId32 "\n",
info.name, ref.name, id, err);
}
}
}
}
void
QLApp::_RemoveFromDeskbar()
{
BDeskbar deskbar;
int32 found_id;
if (deskbar.GetItemInfo("QuickLaunch", &found_id) == B_OK) {
status_t err = deskbar.RemoveItem(found_id);
if (err != B_OK) {
printf("QuickLaunch: Error removing replicant id "
"%" B_PRId32 ": %s\n", found_id, strerror(err));
}
}
}
#pragma mark -- main --
int
main()
{
QLApp app;
app.Run();
return 0;
}