Ticket #6058: filepanel.cpp

File filepanel.cpp, 6.9 KB (added by Karvjorm, 14 years ago)

Localized filepanel.cpp

Line 
1/*
2 * filepanel.cpp - a command line tool to open a BFilePanel and get the result
3 * copyright 2003, Francois Revol, revol@free.fr
4 * LDFLAGS="-lbe -ltracker" make filepanel
5 * return:
6 * 0: the user has selected something,
7 * 1: the user canceled/closed the panel,
8 * 2: an error occured.
9 */
10
11//#define USE_FNMATCH
12
13#ifdef USE_FNMATCH
14#include <fnmatch.h>
15#endif
16#include <stdio.h>
17#include <stdlib.h>
18#include <strings.h>
19#include <Application.h>
20#include <Catalog.h>
21#include <Locale.h>
22#include <Messenger.h>
23#include <Window.h>
24#include <storage/Entry.h>
25#include <storage/FilePanel.h>
26#include <storage/Path.h>
27
28#define APP_SIG "application/x-vnd.mmu_man.filepanel"
29
30#define B_TRANSLATE_CONTEXT "FilePanelApp"
31
32volatile int return_code = 0;
33
34
35class FilePanelApp : public BApplication
36{
37public:
38 FilePanelApp();
39
40 virtual void MessageReceived(BMessage *message);
41 virtual void RefsReceived(BMessage *message);
42};
43
44
45FilePanelApp::FilePanelApp()
46 :BApplication(APP_SIG)
47{
48}
49
50
51void
52FilePanelApp::MessageReceived(BMessage *message)
53{
54 entry_ref e;
55 const char *name;
56 BEntry entry;
57 BPath p;
58
59 //message->PrintToStream();
60 switch (message->what) {
61 case B_SAVE_REQUESTED:
62 message->FindRef("directory", &e);
63 message->FindString("name", &name);
64 entry.SetTo(&e);
65 entry.GetPath(&p);
66 printf("%s/%s\n", p.Path(), name);
67 be_app_messenger.SendMessage(B_QUIT_REQUESTED);
68 break;
69 case B_CANCEL:
70 return_code = 1;
71 be_app_messenger.SendMessage(B_QUIT_REQUESTED);
72 break;
73 default:
74 BApplication::MessageReceived(message);
75 }
76}
77
78
79void
80FilePanelApp::RefsReceived(BMessage *message)
81{
82 entry_ref e;
83 BEntry entry;
84 int i;
85 BPath p;
86// message->PrintToStream();
87 for (i = 0; message->FindRef("refs", i, &e) == B_OK; i++) {
88 entry.SetTo(&e);
89 entry.GetPath(&p);
90 puts(p.Path());
91 }
92 be_app_messenger.SendMessage(B_QUIT_REQUESTED);
93}
94
95
96int
97usage(char *pname, int error)
98{
99 fprintf(stderr, B_TRANSLATE("display a load/save file panel\n"));
100 fprintf(stderr, B_TRANSLATE("usage: %s [--help] [--directory folder] "
101 "[--load|--save] [--title ttl] [--single] [--modal] [--allow pattern] "
102 "[--forbid pattern]\n"), pname);
103 fprintf(stderr, B_TRANSLATE("usage: %s [-h] [-d folder] "
104 "[-l|-s] [-t ttl] [-1] [-m] [-a pattern] "
105 "[-f pattern]\n"), pname);
106 fprintf(stderr, B_TRANSLATE("options:\n"));
107 fprintf(stderr, B_TRANSLATE("short\tlong\t\tdescription\n"));
108 fprintf(stderr, B_TRANSLATE("-h\t--help\t\tdisplay usage\n"));
109 fprintf(stderr, B_TRANSLATE("-d\t--directory\topen at <folder>\n"));
110 fprintf(stderr, B_TRANSLATE("-l\t--load\t\tuse a load FilePanel "
111 "(default)\n"));
112 fprintf(stderr, B_TRANSLATE("-s\t--save\t\tuse a save FilePanel\n"));
113 fprintf(stderr, B_TRANSLATE("-n\t--name\t\tset the default name for "
114 "saving\n"));
115 fprintf(stderr, B_TRANSLATE("-k\t--kind\t\tkind of entries that can be "
116 "opened (flavour):\n\t\t\tany combination of f, d, s (file (default), "
117 "directory, symlink)\n"));
118 fprintf(stderr, B_TRANSLATE("-t\t--title\t\tset the FilePanel window "
119 "title\n"));
120 fprintf(stderr, B_TRANSLATE("-1\t--single\tallow only 1 file to be "
121 "selected\n"));
122 fprintf(stderr, B_TRANSLATE("-m\t--modal\t\tmakes the FilePanel modal\n"));
123#ifndef USE_FNMATCH
124 fprintf(stderr, B_TRANSLATE("-a\t--allow\t\tunimplemented\n"));
125 fprintf(stderr, B_TRANSLATE("-f\t--forbid\tunimplemented\n"));
126#else
127 fprintf(stderr, B_TRANSLATE("-a\t--allow\t\tunimplemented\n"));
128 fprintf(stderr, B_TRANSLATE("-f\t--forbid\tunimplemented\n"));
129#endif
130 return error;
131}
132
133
134int
135main(int argc, char **argv)
136{
137 int i;
138 file_panel_mode fpMode = B_OPEN_PANEL;
139 uint32 nodeFlavour = 0;
140 char *openAt = NULL;
141 char *windowTitle = NULL;
142 bool allowMultiSelect = true;
143 bool makeModal = false;
144 const char *defaultName = NULL;
145 BCatalog fAppCatalog;
146
147 be_locale->GetAppCatalog(&fAppCatalog);
148
149 for (i = 1; i < argc; i++) {
150 if (strncmp(argv[i], "--", 2) && ((*(argv[i]) == '-' &&
151 strlen(argv[i]) != 2) || *(argv[i]) != '-')) {
152 fprintf(stderr, B_TRANSLATE("%s not a valid option\n"),
153 argv[i]);
154 return usage(argv[0], 2);
155 }
156 if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
157 return usage(argv[0], 0);
158 } else if (!strcmp(argv[i], "--directory") || !strcmp(argv[i], "-d")) {
159 if (++i >= argc) {
160 fprintf(stderr, B_TRANSLATE("%s: this option requires a "
161 "parameter\n"), argv[i-1]);
162 return usage(argv[0], 2);
163 }
164 openAt = argv[i];
165 } else if (!strcmp(argv[i], "--load") || !strcmp(argv[i], "-l")) {
166 fpMode = B_OPEN_PANEL;
167 } else if (!strcmp(argv[i], "--save") || !strcmp(argv[i], "-s")) {
168 fpMode = B_SAVE_PANEL;
169 } else if (!strcmp(argv[i], "--name") || !strcmp(argv[i], "-n")) {
170 if (++i >= argc) {
171 fprintf(stderr, B_TRANSLATE("%s: this option requires a "
172 "parameter\n"), argv[i-1]);
173 return usage(argv[0], 2);
174 }
175 defaultName = (const char *)argv[i];
176 } else if (!strcmp(argv[i], "--kind") || !strcmp(argv[i], "-k")) {
177 if (++i >= argc) {
178 fprintf(stderr, B_TRANSLATE("%s: this option requires a "
179 "parameter\n"), argv[i-1]);
180 return usage(argv[0], 2);
181 }
182 if (strchr(argv[i], 'f')) nodeFlavour |= B_FILE_NODE;
183 if (strchr(argv[i], 'd')) nodeFlavour |= B_DIRECTORY_NODE;
184 if (strchr(argv[i], 's')) nodeFlavour |= B_SYMLINK_NODE;
185 } else if (!strcmp(argv[i], "--title") || !strcmp(argv[i], "-t")) {
186 if (++i >= argc) {
187 fprintf(stderr, B_TRANSLATE("%s: this option requires a "
188 "parameter\n"), argv[i-1]);
189 return usage(argv[0], 2);
190 }
191 windowTitle = argv[i];
192 } else if (!strcmp(argv[i], "--single") || !strcmp(argv[i], "-1")) {
193 allowMultiSelect = false;
194 } else if (!strcmp(argv[i], "--modal") || !strcmp(argv[i], "-m")) {
195 makeModal = true;
196 } else if (!strcmp(argv[i], "--allow") || !strcmp(argv[i], "-a")) {
197 if (++i >= argc) {
198 fprintf(stderr, B_TRANSLATE("%s: this option requires a "
199 "parameter\n"), argv[i-1]);
200 return usage(argv[0], 2);
201 }
202 fprintf(stderr, B_TRANSLATE("%s: UNIMPLEMENTED\n"), argv[i-1]);
203 } else if (!strcmp(argv[i], "--forbid") || !strcmp(argv[i], "-f")) {
204 if (++i >= argc) {
205 fprintf(stderr, B_TRANSLATE("%s: this option requires a "
206 "parameter\n"), argv[i-1]);
207 return usage(argv[0], 2);
208 }
209 fprintf(stderr, B_TRANSLATE("%s: UNIMPLEMENTED\n"), argv[i-1]);
210 } else {
211 fprintf(stderr, B_TRANSLATE("%s not a valid option\n"), argv[i]);
212 return usage(argv[0], 2);
213 }
214 }
215 new FilePanelApp;
216 entry_ref panelDir;
217 // THIS LINE makes main() return always 0 no matter which value on return
218 // of exit() ???
219 BFilePanel *fPanel = new BFilePanel(fpMode, NULL, NULL, nodeFlavour,
220 allowMultiSelect, NULL, NULL, makeModal);
221 /**/
222 if (openAt)
223 fPanel->SetPanelDirectory(openAt);
224 if (windowTitle)
225 fPanel->Window()->SetTitle(windowTitle);
226 if (fpMode == B_SAVE_PANEL && defaultName)
227 fPanel->SetSaveText(defaultName);
228
229 fPanel->Show();
230 /**/
231 be_app->Run();
232 delete be_app;
233 // printf("rc = %d\n", return_code);
234 // WTF ??
235 //return 2;
236 // exit(2);
237 exit(return_code);
238 return return_code;
239}
240