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