xref: /haiku/src/bin/filepanel.cpp (revision 893988af824e65e49e55f517b157db8386e8002b)
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 <Messenger.h>
21 #include <Window.h>
22 #include <storage/Entry.h>
23 #include <storage/FilePanel.h>
24 #include <storage/Path.h>
25 
26 #define APP_SIG "application/x-vnd.mmu_man.filepanel"
27 
28 volatile int return_code = 0;
29 
30 class FilePanelApp : public BApplication
31 {
32 public:
33 	FilePanelApp();
34 
35 	virtual void MessageReceived(BMessage *message);
36 	virtual void RefsReceived(BMessage *message);
37 };
38 
39 FilePanelApp::FilePanelApp()
40 	:BApplication(APP_SIG)
41 {
42 }
43 
44 void
45 FilePanelApp::MessageReceived(BMessage *message)
46 {
47 	entry_ref e;
48 	const char *name;
49 	BEntry entry;
50 	BPath p;
51 
52 	//message->PrintToStream();
53 	switch (message->what) {
54 	case B_SAVE_REQUESTED:
55 		message->FindRef("directory", &e);
56 		message->FindString("name", &name);
57 		entry.SetTo(&e);
58 		entry.GetPath(&p);
59 		printf("%s/%s\n", p.Path(), name);
60 		be_app_messenger.SendMessage(B_QUIT_REQUESTED);
61 		break;
62 	case B_CANCEL:
63 		return_code = 1;
64 		be_app_messenger.SendMessage(B_QUIT_REQUESTED);
65 		break;
66 	default:
67 		BApplication::MessageReceived(message);
68 	}
69 }
70 
71 void
72 FilePanelApp::RefsReceived(BMessage *message)
73 {
74 	entry_ref e;
75 	BEntry entry;
76 	int i;
77 	BPath p;
78 //	message->PrintToStream();
79 	for (i = 0; message->FindRef("refs", i, &e) == B_OK; i++) {
80 		entry.SetTo(&e);
81 		entry.GetPath(&p);
82 		puts(p.Path());
83 	}
84 	be_app_messenger.SendMessage(B_QUIT_REQUESTED);
85 }
86 
87 int
88 usage(char *pname, int error)
89 {
90 	fprintf(stderr, "display a load/save file panel\n");
91 	fprintf(stderr, "usage: %s [--help] [--directory folder] [--load|--save] [--title ttl] [--single] [--modal] [--allow pattern] [--forbid pattern]\n", pname);
92 	fprintf(stderr, "usage: %s [-h]     [-d folder]              [-l|-s]     [-t ttl]      [-1]       [-m]      [-a pattern]      [-f pattern]\n", pname);
93 	fprintf(stderr, "options:\n");
94 	fprintf(stderr, "short\tlong\tdescription\n");
95 	fprintf(stderr, "-h\t--help\tdisplay usage\n");
96 	fprintf(stderr, "-d\t--directory\topen at <folder>\n");
97 	fprintf(stderr, "-l\t--load\tuse a load FilePanel (default)\n");
98 	fprintf(stderr, "-s\t--save\tuse a save FilePanel\n");
99 	fprintf(stderr, "-k\t--kind\tkind of entries that can be opened (flavour): any combination of f, d, s (file (default), directory, symlink)\n");
100 	fprintf(stderr, "-t\t--title\tset the FilePanel window title\n");
101 	fprintf(stderr, "-1\t--single\tallow only 1 file to be selected\n");
102 	fprintf(stderr, "-m\t--modal\tmakes the FilePanel modal\n");
103 #ifndef USE_FNMATCH
104 	fprintf(stderr, "-a\t--allow\tunimplemented\n");
105 	fprintf(stderr, "-f\t--forbid\tunimplemented\n");
106 #else
107 	fprintf(stderr, "-a\t--allow\tunimplemented\n");
108 	fprintf(stderr, "-f\t--forbid\tunimplemented\n");
109 #endif
110 	return error;
111 }
112 
113 int
114 main(int argc, char **argv)
115 {
116 	int i;
117 	file_panel_mode fpMode = B_OPEN_PANEL;
118 	uint32 nodeFlavour = 0;
119 	char *openAt = NULL;
120 	char *windowTitle = NULL;
121 	bool allowMultiSelect = true;
122 	bool makeModal = false;
123 
124 	for (i = 1; i < argc; i++) {
125 		if (strncmp(argv[i], "--", 2) && ((*(argv[i]) == '-' && strlen(argv[i]) != 2) || *(argv[i]) != '-')) {
126 			fprintf(stderr, "%s not a valid option\n", argv[i]);
127 			return usage(argv[0], 2);
128 		}
129 		if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
130 			return usage(argv[0], 0);
131 		} else if (!strcmp(argv[i], "--directory") || !strcmp(argv[i], "-d")) {
132 			if (++i >= argc) {
133 				fprintf(stderr, "%s: this option requires a parameter\n", argv[i-1]);
134 				return usage(argv[0], 2);
135 			}
136 			openAt = argv[i];
137 		} else if (!strcmp(argv[i], "--load") || !strcmp(argv[i], "-l")) {
138 			fpMode = B_OPEN_PANEL;
139 		} else if (!strcmp(argv[i], "--save") || !strcmp(argv[i], "-s")) {
140 			fpMode = B_SAVE_PANEL;
141 		} else if (!strcmp(argv[i], "--kind") || !strcmp(argv[i], "-k")) {
142 			if (++i >= argc) {
143 				fprintf(stderr, "%s: this option requires a parameter\n", argv[i-1]);
144 				return usage(argv[0], 2);
145 			}
146 			if (strchr(argv[i], 'f')) nodeFlavour |= B_FILE_NODE;
147 			if (strchr(argv[i], 'd')) nodeFlavour |= B_DIRECTORY_NODE;
148 			if (strchr(argv[i], 's')) nodeFlavour |= B_SYMLINK_NODE;
149 		} else if (!strcmp(argv[i], "--title") || !strcmp(argv[i], "-t")) {
150 			if (++i >= argc) {
151 				fprintf(stderr, "%s: this option requires a parameter\n", argv[i-1]);
152 				return usage(argv[0], 2);
153 			}
154 			windowTitle = argv[i];
155 		} else if (!strcmp(argv[i], "--single") || !strcmp(argv[i], "-1")) {
156 			allowMultiSelect = false;
157 		} else if (!strcmp(argv[i], "--modal") || !strcmp(argv[i], "-m")) {
158 			makeModal = true;
159 		} else if (!strcmp(argv[i], "--allow") || !strcmp(argv[i], "-a")) {
160 			if (++i >= argc) {
161 				fprintf(stderr, "%s: this option requires a parameter\n", argv[i-1]);
162 				return usage(argv[0], 2);
163 			}
164 			fprintf(stderr, "%s: UNIMPLEMENTED\n", argv[i-1]);
165 		} else if (!strcmp(argv[i], "--forbid") || !strcmp(argv[i], "-f")) {
166 			if (++i >= argc) {
167 				fprintf(stderr, "%s: this option requires a parameter\n", argv[i-1]);
168 				return usage(argv[0], 2);
169 			}
170 			fprintf(stderr, "%s: UNIMPLEMENTED\n", argv[i-1]);
171 		} else {
172 			fprintf(stderr, "%s not a valid option\n", argv[i]);
173 			return usage(argv[0], 2);
174 		}
175 	}
176 	new FilePanelApp;
177 	entry_ref panelDir;
178 // THIS LINE makes main() return always 0 no matter which value on return of exit() ???
179 	BFilePanel *fPanel = new BFilePanel(fpMode, NULL, NULL, nodeFlavour, allowMultiSelect, NULL, NULL, makeModal);
180 /**/
181 	if (openAt)
182 		fPanel->SetPanelDirectory(openAt);
183 	if (windowTitle)
184 		fPanel->Window()->SetTitle(windowTitle);
185 
186 	fPanel->Show();
187 /**/
188 	be_app->Run();
189 	delete be_app;
190 //	printf("rc = %d\n", return_code);
191 // WTF ??
192 //return 2;
193 //	exit(2);
194 	exit(return_code);
195 	return return_code;
196 }
197 
198