Line 0
Link Here
|
|
|
1 |
// Application.cc for Fluxbox Window Manager |
2 |
// Copyright (c) 2002 Xavier Brouckaert |
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 |
17 |
// THE 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 |
//use GNU extensions |
23 |
#ifndef _GNU_SOURCE |
24 |
#define _GNU_SOURCE |
25 |
#endif // _GNU_SOURCE |
26 |
|
27 |
#include <iostream> |
28 |
#include <string> |
29 |
#include <memory> |
30 |
#include <sstream> |
31 |
#include <fstream> |
32 |
#include <stdio.h> |
33 |
|
34 |
#include "Application.hh" |
35 |
#include "fluxbox.hh" |
36 |
|
37 |
#ifndef MAXPATHLEN |
38 |
#define MAXPATHLEN 255 |
39 |
#endif // MAXPATHLEN |
40 |
|
41 |
Application::Application() { |
42 |
workspace_remember = |
43 |
dimensions_remember = |
44 |
position_remember = |
45 |
stuckstate_remember = |
46 |
decostate_remember = |
47 |
shadedstate_remember = |
48 |
tabstate_remember = |
49 |
jumpworkspace_remember = |
50 |
save_on_close_remember = false; |
51 |
} |
52 |
|
53 |
Applications::Applications() { |
54 |
load(); |
55 |
} |
56 |
|
57 |
Application* Applications::add(char* app_name) { |
58 |
if (!app_name) |
59 |
return NULL; |
60 |
Application* a = new Application(); |
61 |
apps[app_name] = a; |
62 |
return a; |
63 |
} |
64 |
|
65 |
Application* Applications::find(char* app_name) { |
66 |
if (!app_name) |
67 |
return NULL; |
68 |
Apps::iterator i = apps.find(app_name); |
69 |
if (i!=apps.end()) |
70 |
return i->second; |
71 |
else |
72 |
return NULL; |
73 |
} |
74 |
|
75 |
void Applications::parseApp(ifstream &file, Application *a) { |
76 |
string line; |
77 |
|
78 |
while (! file.eof()) { |
79 |
if (getline(file, line)) { |
80 |
if (line[0] != '#') { //the line is commented |
81 |
int parse_pos = 0, err = 0; |
82 |
std::string str_key, str_label; |
83 |
err = StringUtil::getStringBetween(str_key, line.c_str(), '[', ']'); |
84 |
if (err > 0 ) { |
85 |
parse_pos += err; |
86 |
err = StringUtil::getStringBetween(str_label, line.c_str() + parse_pos, '{', '}'); |
87 |
if (err>0) { |
88 |
parse_pos += err; |
89 |
} |
90 |
} else |
91 |
continue; //read next line |
92 |
if (!str_key.size()) |
93 |
continue; //read next line |
94 |
if (str_key == "Workspace") { |
95 |
unsigned int w; |
96 |
istringstream iss(str_label.c_str()); |
97 |
iss >> w; |
98 |
a->rememberWorkspace(w); |
99 |
} else if (str_key == "Dimensions") { |
100 |
unsigned int h,w; |
101 |
istringstream iss(str_label.c_str()); |
102 |
iss >> w >> h; |
103 |
a->rememberDimensions(w,h); |
104 |
} else if (str_key == "Position") { |
105 |
unsigned int x,y; |
106 |
istringstream iss(str_label); |
107 |
iss >> x >> y; |
108 |
a->rememberPosition(x,y); |
109 |
} else if (str_key == "Shaded") { |
110 |
a->rememberShadedstate((str_label=="yes")); |
111 |
} else if (str_key == "Tab") { |
112 |
a->rememberTabstate((str_label=="yes")); |
113 |
} else if (str_key == "Deco") { |
114 |
if (str_label == "NONE") { |
115 |
a->rememberDecostate(FluxboxWindow::DECOR_NONE); |
116 |
} else if (str_label == "NORMAL") { |
117 |
a->rememberDecostate(FluxboxWindow::DECOR_NORMAL); |
118 |
} else if (str_label == "TINY") { |
119 |
a->rememberDecostate(FluxboxWindow::DECOR_TINY); |
120 |
} else if (str_label == "TOOL") { |
121 |
a->rememberDecostate(FluxboxWindow::DECOR_TOOL); |
122 |
} |
123 |
} else if (str_key == "Sticky") { |
124 |
a->rememberStuckstate((str_label=="yes")); |
125 |
} else if (str_key == "Jump") { |
126 |
a->rememberJumpworkspace((str_label=="yes")); |
127 |
} else if (str_key == "Close") { |
128 |
a->rememberSave((str_label=="yes")); |
129 |
} else if (str_key == "end") { |
130 |
return; |
131 |
} else { |
132 |
cerr << "Unsupported apps key = " << str_key << endl; |
133 |
} |
134 |
} |
135 |
} |
136 |
} |
137 |
} |
138 |
|
139 |
void Applications::load() { |
140 |
cerr << "Loading apps file..." << endl; |
141 |
string apps_string = getenv("HOME")+string("/.")+RC_PATH+string("/")+"apps"; |
142 |
ifstream apps_file(apps_string.c_str()); |
143 |
if (!apps_file.fail()) { |
144 |
if (!apps_file.eof()) { |
145 |
string line; |
146 |
int row = 0; |
147 |
while (getline(apps_file, line) && ! apps_file.eof()) { |
148 |
row++; |
149 |
if (line[0] != '#') { |
150 |
string key; |
151 |
int pos=0; |
152 |
int err = StringUtil::getStringBetween(key, line.c_str(), '[', ']'); |
153 |
|
154 |
if (key == "app") { |
155 |
pos += err; |
156 |
string label; |
157 |
err = StringUtil::getStringBetween(label, line.c_str()+pos, '(', ')'); |
158 |
if (err>0) { |
159 |
Application *a; |
160 |
Apps::iterator i = apps.find(label); |
161 |
if (i==apps.end()) { |
162 |
a = new Application(); |
163 |
apps[label] = a; |
164 |
} else |
165 |
a = i->second; |
166 |
parseApp(apps_file, a); |
167 |
} else |
168 |
cerr<<"Error in apps file. Line("<<row<<")"<<endl; |
169 |
} |
170 |
} |
171 |
} |
172 |
} else { |
173 |
cerr<<__FILE__<<"("<<__LINE__<< "Empty apps file" << endl; |
174 |
} |
175 |
} else { |
176 |
cerr << "apps file failure" << endl; |
177 |
} |
178 |
} |
179 |
|
180 |
void Applications::save() { |
181 |
cerr << "Saving apps file..." << endl; |
182 |
string apps_string = getenv("HOME")+string("/.")+RC_PATH+string("/")+"apps"; |
183 |
ofstream apps_file(apps_string.c_str()); |
184 |
Apps::iterator it = apps.begin(); |
185 |
Apps::iterator it_end = apps.end(); |
186 |
for (; it != it_end; ++it) { |
187 |
apps_file << "[app] (" << it->first << ")" << endl; |
188 |
Application *a = it->second; |
189 |
if (a->workspace_remember) { |
190 |
apps_file << " [Workspace]\t{" << a->workspace << "}" << endl; |
191 |
} |
192 |
if (a->dimensions_remember) { |
193 |
apps_file << " [Dimensions]\t{" << a->w << " " << a->h << "}" << endl; |
194 |
} |
195 |
if (a->position_remember) { |
196 |
apps_file << " [Position]\t{" << a->x << " " << a->y << "}" << endl; |
197 |
} |
198 |
if (a->shadedstate_remember) { |
199 |
apps_file << " [Shaded]\t{" << ((a->shadedstate)?"yes":"no") << "}" << endl; |
200 |
} |
201 |
if (a->tabstate_remember) { |
202 |
apps_file << " [Tab]\t\t{" << ((a->tabstate)?"yes":"no") << "}" << endl; |
203 |
} |
204 |
if (a->decostate_remember) { |
205 |
switch (a->decostate) { |
206 |
case (FluxboxWindow::DECOR_NONE) : |
207 |
apps_file << " [Deco]\t{NONE}" << endl; break; |
208 |
case (FluxboxWindow::DECOR_NORMAL) : |
209 |
apps_file << " [Deco]\t{NORMAL}" << endl; break; |
210 |
case (FluxboxWindow::DECOR_TINY) : |
211 |
apps_file << " [Deco]\t{TINY}" << endl; break; |
212 |
case (FluxboxWindow::DECOR_TOOL) : |
213 |
apps_file << " [Deco]\t{TOOL}" << endl; break; |
214 |
} |
215 |
} |
216 |
if (a->stuckstate_remember) { |
217 |
apps_file << " [Sticky]\t{" << ((a->stuckstate)?"yes":"no") << "}" << endl; |
218 |
} |
219 |
if (a->jumpworkspace_remember) { |
220 |
apps_file << " [Jump]\t{" << ((a->jumpworkspace)?"yes":"no") << "}" << endl; |
221 |
} |
222 |
if (a->save_on_close_remember) { |
223 |
apps_file << " [Close]\t{" << ((a->save_on_close)?"yes":"no") << "}" << endl; |
224 |
} |
225 |
apps_file << "[end]" << endl; |
226 |
} |
227 |
} |