18a21dd58SFrançois Revol/*
28a21dd58SFrançois Revol * filepanel.cpp - a command line tool to open a BFilePanel and get the result
38a21dd58SFrançois Revol * copyright 2003, Francois Revol, revol@free.fr
48a21dd58SFrançois Revol * LDFLAGS="-lbe -ltracker" make filepanel
58a21dd58SFrançois Revol * return:
68a21dd58SFrançois Revol * 0: the user has selected something,
78a21dd58SFrançois Revol * 1: the user canceled/closed the panel,
88a21dd58SFrançois Revol * 2: an error occured.
98a21dd58SFrançois Revol */
108a21dd58SFrançois Revol
118a21dd58SFrançois Revol//#define USE_FNMATCH
128a21dd58SFrançois Revol
138a21dd58SFrançois Revol#ifdef USE_FNMATCH
148a21dd58SFrançois Revol#include <fnmatch.h>
158a21dd58SFrançois Revol#endif
168a21dd58SFrançois Revol#include <stdio.h>
178a21dd58SFrançois Revol#include <stdlib.h>
188a21dd58SFrançois Revol#include <strings.h>
198a21dd58SFrançois Revol#include <Application.h>
206c5a1342SAdrien Destugues#include <Catalog.h>
216c5a1342SAdrien Destugues#include <Locale.h>
228a21dd58SFrançois Revol#include <Messenger.h>
238a21dd58SFrançois Revol#include <Window.h>
248a21dd58SFrançois Revol#include <storage/Entry.h>
258a21dd58SFrançois Revol#include <storage/FilePanel.h>
268a21dd58SFrançois Revol#include <storage/Path.h>
278a21dd58SFrançois Revol
288a21dd58SFrançois Revol#define APP_SIG "application/x-vnd.mmu_man.filepanel"
298a21dd58SFrançois Revol
30546208a5SOliver Tappe#undef B_TRANSLATION_CONTEXT
31546208a5SOliver Tappe#define B_TRANSLATION_CONTEXT "FilePanelApp"
326c5a1342SAdrien Destugues
336c5a1342SAdrien Destugues
348a21dd58SFrançois Revolvolatile int return_code = 0;
358a21dd58SFrançois Revol
368a21dd58SFrançois Revolclass FilePanelApp : public BApplication
378a21dd58SFrançois Revol{
388a21dd58SFrançois Revolpublic:
398a21dd58SFrançois Revol	FilePanelApp();
408a21dd58SFrançois Revol
418a21dd58SFrançois Revol	virtual void MessageReceived(BMessage *message);
428a21dd58SFrançois Revol	virtual void RefsReceived(BMessage *message);
438a21dd58SFrançois Revol};
448a21dd58SFrançois Revol
456c5a1342SAdrien Destugues
468a21dd58SFrançois RevolFilePanelApp::FilePanelApp()
478a21dd58SFrançois Revol	:BApplication(APP_SIG)
488a21dd58SFrançois Revol{
498a21dd58SFrançois Revol}
508a21dd58SFrançois Revol
516c5a1342SAdrien Destugues
528a21dd58SFrançois Revolvoid
538a21dd58SFrançois RevolFilePanelApp::MessageReceived(BMessage *message)
548a21dd58SFrançois Revol{
558a21dd58SFrançois Revol	entry_ref e;
568a21dd58SFrançois Revol	const char *name;
578a21dd58SFrançois Revol	BEntry entry;
588a21dd58SFrançois Revol	BPath p;
598a21dd58SFrançois Revol
609226faeaSFrançois Revol	//message->PrintToStream();
618a21dd58SFrançois Revol	switch (message->what) {
628a21dd58SFrançois Revol	case B_SAVE_REQUESTED:
638a21dd58SFrançois Revol		message->FindRef("directory", &e);
648a21dd58SFrançois Revol		message->FindString("name", &name);
658a21dd58SFrançois Revol		entry.SetTo(&e);
668a21dd58SFrançois Revol		entry.GetPath(&p);
678a21dd58SFrançois Revol		printf("%s/%s\n", p.Path(), name);
688a21dd58SFrançois Revol		be_app_messenger.SendMessage(B_QUIT_REQUESTED);
698a21dd58SFrançois Revol		break;
708a21dd58SFrançois Revol	case B_CANCEL:
718a21dd58SFrançois Revol		return_code = 1;
728a21dd58SFrançois Revol		be_app_messenger.SendMessage(B_QUIT_REQUESTED);
738a21dd58SFrançois Revol		break;
748a21dd58SFrançois Revol	default:
758a21dd58SFrançois Revol		BApplication::MessageReceived(message);
768a21dd58SFrançois Revol	}
778a21dd58SFrançois Revol}
788a21dd58SFrançois Revol
796c5a1342SAdrien Destugues
808a21dd58SFrançois Revolvoid
818a21dd58SFrançois RevolFilePanelApp::RefsReceived(BMessage *message)
828a21dd58SFrançois Revol{
838a21dd58SFrançois Revol	entry_ref e;
848a21dd58SFrançois Revol	BEntry entry;
858a21dd58SFrançois Revol	int i;
868a21dd58SFrançois Revol	BPath p;
878a21dd58SFrançois Revol//	message->PrintToStream();
888a21dd58SFrançois Revol	for (i = 0; message->FindRef("refs", i, &e) == B_OK; i++) {
898a21dd58SFrançois Revol		entry.SetTo(&e);
908a21dd58SFrançois Revol		entry.GetPath(&p);
918a21dd58SFrançois Revol		puts(p.Path());
928a21dd58SFrançois Revol	}
938a21dd58SFrançois Revol	be_app_messenger.SendMessage(B_QUIT_REQUESTED);
948a21dd58SFrançois Revol}
958a21dd58SFrançois Revol
966c5a1342SAdrien Destugues
979226faeaSFrançois Revolint
989226faeaSFrançois Revolusage(char *pname, int error)
998a21dd58SFrançois Revol{
1006c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("display a load/save file panel\n"));
1016c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("usage: %s [--help] [--directory folder] "
1026c5a1342SAdrien Destugues		"[--load|--save] [--title ttl] [--single] [--modal] [--allow pattern] "
1036c5a1342SAdrien Destugues		"[--forbid pattern]\n"), pname);
1046c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("usage: %s [-h]     [-d folder]              "
1056c5a1342SAdrien Destugues		"[-l|-s]     [-t ttl]      [-1]       [-m]      [-a pattern]      "
1066c5a1342SAdrien Destugues		"[-f pattern]\n"), pname);
1076c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("options:\n"));
1086c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("short\tlong\tdescription\n"));
1096c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("-h\t--help\tdisplay usage\n"));
1106c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("-d\t--directory\topen at <folder>\n"));
1116c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("-l\t--load\tuse a load FilePanel "
1126c5a1342SAdrien Destugues		"(default)\n"));
1136c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("-s\t--save\tuse a save FilePanel\n"));
1146c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("-n\t--name\tset the default name for "
1156c5a1342SAdrien Destugues		"saving\n"));
1166c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("-k\t--kind\tkind of entries that can be "
1176c5a1342SAdrien Destugues		"opened (flavour): any combination of f, d, s (file (default), "
1186c5a1342SAdrien Destugues		"directory, symlink)\n"));
1196c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("-t\t--title\tset the FilePanel window "
1206c5a1342SAdrien Destugues		"title\n"));
1216c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("-1\t--single\tallow only 1 file to be "
1226c5a1342SAdrien Destugues		"selected\n"));
1236c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("-m\t--modal\tmakes the FilePanel modal\n"));
1248a21dd58SFrançois Revol#ifndef USE_FNMATCH
1256c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("-a\t--allow\tunimplemented\n"));
1266c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("-f\t--forbid\tunimplemented\n"));
1278a21dd58SFrançois Revol#else
1286c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("-a\t--allow\tunimplemented\n"));
1296c5a1342SAdrien Destugues	fprintf(stderr, B_TRANSLATE("-f\t--forbid\tunimplemented\n"));
1308a21dd58SFrançois Revol#endif
1318a21dd58SFrançois Revol	return error;
1328a21dd58SFrançois Revol}
1338a21dd58SFrançois Revol
1346c5a1342SAdrien Destugues
1359226faeaSFrançois Revolint
1369226faeaSFrançois Revolmain(int argc, char **argv)
1378a21dd58SFrançois Revol{
1388a21dd58SFrançois Revol	int i;
1398a21dd58SFrançois Revol	file_panel_mode fpMode = B_OPEN_PANEL;
1408a21dd58SFrançois Revol	uint32 nodeFlavour = 0;
1418a21dd58SFrançois Revol	char *openAt = NULL;
1428a21dd58SFrançois Revol	char *windowTitle = NULL;
1438a21dd58SFrançois Revol	bool allowMultiSelect = true;
1448a21dd58SFrançois Revol	bool makeModal = false;
14569955dd9SFrançois Revol	const char *defaultName = NULL;
1468a21dd58SFrançois Revol
1478a21dd58SFrançois Revol	for (i = 1; i < argc; i++) {
1486c5a1342SAdrien Destugues		if (strncmp(argv[i], "--", 2) && ((*(argv[i]) == '-' &&
1496c5a1342SAdrien Destugues			strlen(argv[i]) != 2) || *(argv[i]) != '-')) {
1506c5a1342SAdrien Destugues			fprintf(stderr, B_TRANSLATE("%s not a valid option\n"), argv[i]);
1518a21dd58SFrançois Revol			return usage(argv[0], 2);
1528a21dd58SFrançois Revol		}
1538a21dd58SFrançois Revol		if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
1548a21dd58SFrançois Revol			return usage(argv[0], 0);
1558a21dd58SFrançois Revol		} else if (!strcmp(argv[i], "--directory") || !strcmp(argv[i], "-d")) {
1568a21dd58SFrançois Revol			if (++i >= argc) {
1576c5a1342SAdrien Destugues				fprintf(stderr, B_TRANSLATE("%s: this option requires a "
1586c5a1342SAdrien Destugues					"parameter\n"), argv[i-1]);
1598a21dd58SFrançois Revol				return usage(argv[0], 2);
1608a21dd58SFrançois Revol			}
1618a21dd58SFrançois Revol			openAt = argv[i];
1628a21dd58SFrançois Revol		} else if (!strcmp(argv[i], "--load") || !strcmp(argv[i], "-l")) {
1638a21dd58SFrançois Revol			fpMode = B_OPEN_PANEL;
1648a21dd58SFrançois Revol		} else if (!strcmp(argv[i], "--save") || !strcmp(argv[i], "-s")) {
1658a21dd58SFrançois Revol			fpMode = B_SAVE_PANEL;
16669955dd9SFrançois Revol		} else if (!strcmp(argv[i], "--name") || !strcmp(argv[i], "-n")) {
16769955dd9SFrançois Revol			if (++i >= argc) {
1686c5a1342SAdrien Destugues				fprintf(stderr, B_TRANSLATE("%s: this option requires a "
1696c5a1342SAdrien Destugues					"parameter\n"), argv[i-1]);
17069955dd9SFrançois Revol				return usage(argv[0], 2);
17169955dd9SFrançois Revol			}
17269955dd9SFrançois Revol			defaultName = (const char *)argv[i];
1738a21dd58SFrançois Revol		} else if (!strcmp(argv[i], "--kind") || !strcmp(argv[i], "-k")) {
1748a21dd58SFrançois Revol			if (++i >= argc) {
1756c5a1342SAdrien Destugues				fprintf(stderr, B_TRANSLATE("%s: this option requires a "
1766c5a1342SAdrien Destugues					"parameter\n"), argv[i-1]);
1778a21dd58SFrançois Revol				return usage(argv[0], 2);
1788a21dd58SFrançois Revol			}
1798a21dd58SFrançois Revol			if (strchr(argv[i], 'f')) nodeFlavour |= B_FILE_NODE;
1808a21dd58SFrançois Revol			if (strchr(argv[i], 'd')) nodeFlavour |= B_DIRECTORY_NODE;
1818a21dd58SFrançois Revol			if (strchr(argv[i], 's')) nodeFlavour |= B_SYMLINK_NODE;
1828a21dd58SFrançois Revol		} else if (!strcmp(argv[i], "--title") || !strcmp(argv[i], "-t")) {
1838a21dd58SFrançois Revol			if (++i >= argc) {
1846c5a1342SAdrien Destugues				fprintf(stderr, B_TRANSLATE("%s: this option requires a "
1856c5a1342SAdrien Destugues					"parameter\n"), argv[i-1]);
1868a21dd58SFrançois Revol				return usage(argv[0], 2);
1878a21dd58SFrançois Revol			}
1888a21dd58SFrançois Revol			windowTitle = argv[i];
1898a21dd58SFrançois Revol		} else if (!strcmp(argv[i], "--single") || !strcmp(argv[i], "-1")) {
1908a21dd58SFrançois Revol			allowMultiSelect = false;
1918a21dd58SFrançois Revol		} else if (!strcmp(argv[i], "--modal") || !strcmp(argv[i], "-m")) {
1928a21dd58SFrançois Revol			makeModal = true;
1938a21dd58SFrançois Revol		} else if (!strcmp(argv[i], "--allow") || !strcmp(argv[i], "-a")) {
1948a21dd58SFrançois Revol			if (++i >= argc) {
1956c5a1342SAdrien Destugues				fprintf(stderr, B_TRANSLATE("%s: this option requires a "
1966c5a1342SAdrien Destugues					"parameter\n"), argv[i-1]);
1978a21dd58SFrançois Revol				return usage(argv[0], 2);
1988a21dd58SFrançois Revol			}
1996c5a1342SAdrien Destugues			fprintf(stderr, B_TRANSLATE("%s: UNIMPLEMENTED\n"), argv[i-1]);
2008a21dd58SFrançois Revol		} else if (!strcmp(argv[i], "--forbid") || !strcmp(argv[i], "-f")) {
2018a21dd58SFrançois Revol			if (++i >= argc) {
2026c5a1342SAdrien Destugues				fprintf(stderr, B_TRANSLATE("%s: this option requires a "
2036c5a1342SAdrien Destugues					"parameter\n"), argv[i-1]);
2048a21dd58SFrançois Revol				return usage(argv[0], 2);
2058a21dd58SFrançois Revol			}
2066c5a1342SAdrien Destugues			fprintf(stderr, B_TRANSLATE("%s: UNIMPLEMENTED\n"), argv[i-1]);
2078a21dd58SFrançois Revol		} else {
2086c5a1342SAdrien Destugues			fprintf(stderr, B_TRANSLATE("%s not a valid option\n"), argv[i]);
2098a21dd58SFrançois Revol			return usage(argv[0], 2);
2108a21dd58SFrançois Revol		}
2118a21dd58SFrançois Revol	}
2128a21dd58SFrançois Revol	new FilePanelApp;
2138a21dd58SFrançois Revol	entry_ref panelDir;
2146c5a1342SAdrien Destugues// THIS LINE makes main() return always 0 no matter which value on return of
2156c5a1342SAdrien Destugues// exit() ???
2166c5a1342SAdrien Destugues	BFilePanel *fPanel = new BFilePanel(fpMode, NULL, NULL, nodeFlavour,
2176c5a1342SAdrien Destugues		allowMultiSelect, NULL, NULL, makeModal);
2188a21dd58SFrançois Revol/**/
2198a21dd58SFrançois Revol	if (openAt)
2208a21dd58SFrançois Revol		fPanel->SetPanelDirectory(openAt);
2218a21dd58SFrançois Revol	if (windowTitle)
2228a21dd58SFrançois Revol		fPanel->Window()->SetTitle(windowTitle);
22369955dd9SFrançois Revol	if (fpMode == B_SAVE_PANEL && defaultName)
22469955dd9SFrançois Revol		fPanel->SetSaveText(defaultName);
2258a21dd58SFrançois Revol
2268a21dd58SFrançois Revol	fPanel->Show();
2278a21dd58SFrançois Revol/**/
2288a21dd58SFrançois Revol	be_app->Run();
2298a21dd58SFrançois Revol	delete be_app;
2308a21dd58SFrançois Revol//	printf("rc = %d\n", return_code);
2318a21dd58SFrançois Revol// WTF ??
2328a21dd58SFrançois Revol//return 2;
2339226faeaSFrançois Revol//	exit(2);
2348a21dd58SFrançois Revol	exit(return_code);
2358a21dd58SFrançois Revol	return return_code;
2368a21dd58SFrançois Revol}
2378a21dd58SFrançois Revol
238