xref: /haiku/src/apps/text_search/GrepApp.cpp (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 /*
2  * Copyright (c) 1998-2007 Matthijs Hollemans
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "GrepApp.h"
24 
25 #include <stdio.h>
26 
27 #include <Entry.h>
28 
29 #include "GlobalDefs.h"
30 #include "GrepWindow.h"
31 #include "Model.h"
32 
33 
34 GrepApp::GrepApp()
35 	: BApplication(APP_SIGNATURE),
36 	  fGotArgvOnStartup(false),
37 	  fGotRefsOnStartup(false),
38 	  fQuitter(NULL)
39 {
40 }
41 
42 
43 GrepApp::~GrepApp()
44 {
45 	delete fQuitter;
46 }
47 
48 
49 void
50 GrepApp::ArgvReceived(int32 argc, char** argv)
51 {
52 	fGotArgvOnStartup = true;
53 
54 	BMessage message(B_REFS_RECEIVED);
55 	int32 refCount = 0;
56 
57 	for (int32 i = 1; i < argc; i++) {
58 		BEntry entry(argv[i]);
59 		entry_ref ref;
60 		entry.GetRef(&ref);
61 
62 		if (entry.Exists()) {
63 			message.AddRef("refs", &ref);
64 			refCount += 1;
65 		} else
66 			printf("%s: File not found: %s\n", argv[0], argv[i]);
67 	}
68 
69 	if (refCount > 0)
70 		RefsReceived(&message);
71 }
72 
73 
74 void
75 GrepApp::RefsReceived(BMessage* message)
76 {
77 	if (IsLaunching())
78 		fGotRefsOnStartup = true;
79 
80 	new GrepWindow(message);
81 }
82 
83 
84 void
85 GrepApp::ReadyToRun()
86 {
87 	if (!fGotArgvOnStartup && !fGotRefsOnStartup)
88 		_NewUnfocusedGrepWindow();
89 
90 	// TODO: stippi: I don't understand what this is supposed to do:
91 	if (fGotArgvOnStartup && !fGotRefsOnStartup)
92 		PostMessage(B_QUIT_REQUESTED);
93 }
94 
95 
96 void
97 GrepApp::MessageReceived(BMessage* message)
98 {
99 	switch (message->what) {
100 		case B_SILENT_RELAUNCH:
101 			_NewUnfocusedGrepWindow();
102 			break;
103 
104 		case MSG_TRY_QUIT:
105 			_TryQuit();
106 			break;
107 
108 		default:
109 			BApplication::MessageReceived(message);
110 			break;
111 	}
112 }
113 
114 
115 void
116 GrepApp::_TryQuit()
117 {
118 	if (CountWindows() == 0)
119 		PostMessage(B_QUIT_REQUESTED);
120 
121 	if (CountWindows() == 1 && fQuitter == NULL) {
122 		fQuitter = new BMessageRunner(be_app_messenger,
123 			new BMessage(MSG_TRY_QUIT), 200000, -1);
124 	}
125 }
126 
127 
128 void
129 GrepApp::_NewUnfocusedGrepWindow()
130 {
131 	BMessage emptyMessage;
132 	new GrepWindow(&emptyMessage);
133 }
134