From a11f505d56b683427e963fec83fe7ed5a73fb340 Mon Sep 17 00:00:00 2001 From: shatty Date: Mon, 18 Aug 2003 08:30:32 +0000 Subject: [PATCH] round two on FileType app/add-on : ability to open file(s), set and save mime types git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4303 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/add-ons/tracker/filetype/FileTypeApp.cpp | 53 +++++++++++++-- src/add-ons/tracker/filetype/FileTypeApp.h | 2 +- src/add-ons/tracker/filetype/FileTypeView.cpp | 67 ++++++++++++++++--- src/add-ons/tracker/filetype/FileTypeView.h | 23 ++++--- .../tracker/filetype/FileTypeWindow.cpp | 21 +++++- src/add-ons/tracker/filetype/FileTypeWindow.h | 1 + 6 files changed, 136 insertions(+), 31 deletions(-) diff --git a/src/add-ons/tracker/filetype/FileTypeApp.cpp b/src/add-ons/tracker/filetype/FileTypeApp.cpp index 12c7327baf..0770abe968 100644 --- a/src/add-ons/tracker/filetype/FileTypeApp.cpp +++ b/src/add-ons/tracker/filetype/FileTypeApp.cpp @@ -46,7 +46,13 @@ FileTypeApp::RefsReceived(BMessage * message) int32 i; entry_ref ref; while (message->FindRef("refs",i++,&ref) == B_OK) { - entryList.AddItem(new BEntry(&ref,true)); + BEntry * entry = new BEntry(&ref,true); + if (!entry || (entry->InitCheck() != B_OK) || (!entry->Exists())) { + // ignore bogus refs + delete entry; + continue; + } + entryList.AddItem(entry); } if (entryList.CountItems() == 0) { return; @@ -62,9 +68,11 @@ FileTypeApp::PrintUsage(const char * execname) { if (execname == 0) { execname = "FileType"; } - fprintf(stderr,"Usage: %s [FILES]\n",execname); + fprintf(stderr,"Usage: %s [OPTIONS] [FILES]\n",execname); fprintf(stderr,"Open a FileType window for the given FILES.\n"); fprintf(stderr,"\n"); + fprintf(stderr," -h, --help print this help\n"); + fprintf(stderr,"\n"); fprintf(stderr,"Report bugs to shatty@myrealbox.com\n"); fprintf(stderr,"\n"); } @@ -92,15 +100,45 @@ FileTypeApp::ArgvReceived(int32 argc, const char * argv[], const char * cwd) continue; } - entry_ref ref; - if (get_ref_for_path(path.Path(), &ref) != B_OK) { - printf("get_ref_for_path failed: \""); - printf("%s",path.Path()); + BEntry * entry = new BEntry(path.Path(),true); + if (!entry || (entry->InitCheck() != B_OK)) { + printf("failed to allocate BEntry: \""); + if (argv[i][0] == '/') { + printf("%s",argv[i]); + } else { + printf("%s/%s",cwd,argv[i]); + } printf("\".\n"); + delete entry; continue; } - entryList.AddItem(new BEntry(&ref,true)); + if (!entry->Exists()) { + if ((strcmp(argv[i],"-h") == 0) || + (strcmp(argv[i],"-H") == 0) || + (strcmp(argv[i],"-help") == 0) || + (strcmp(argv[i],"--help") == 0)) { + void * item; + for (int32 i = 0 ; (i < entryList.CountItems()) ; i++) { + delete entryList.ItemAt(i); + } + entryList.MakeEmpty(); + delete entry; + break; + } else { + printf("file does not exist: \""); + if (argv[i][0] == '/') { + printf("%s",argv[i]); + } else { + printf("%s/%s",cwd,argv[i]); + } + printf("\".\n"); + delete entry; + continue; + } + } + + entryList.AddItem(entry); } if (entryList.CountItems() == 0) { PrintUsage(argv[0]); @@ -111,6 +149,7 @@ FileTypeApp::ArgvReceived(int32 argc, const char * argv[], const char * cwd) printf("Failed to create FileTypeWindow\n"); return; } + fWindow = window; fArgvOkay = true; } diff --git a/src/add-ons/tracker/filetype/FileTypeApp.h b/src/add-ons/tracker/filetype/FileTypeApp.h index 9bfbf55c26..289dd14755 100644 --- a/src/add-ons/tracker/filetype/FileTypeApp.h +++ b/src/add-ons/tracker/filetype/FileTypeApp.h @@ -27,6 +27,6 @@ private: bool fArgvOkay; }; -extern FileTypeApp * file_types_app; +extern FileTypeApp * file_type_app; #endif // FILE_TYPE_APP diff --git a/src/add-ons/tracker/filetype/FileTypeView.cpp b/src/add-ons/tracker/filetype/FileTypeView.cpp index 552963f3e1..f87d4fc465 100644 --- a/src/add-ons/tracker/filetype/FileTypeView.cpp +++ b/src/add-ons/tracker/filetype/FileTypeView.cpp @@ -1,14 +1,46 @@ +#include + #include FileTypeView::FileTypeView(BRect viewFrame) : BView(viewFrame, "FileTypeView", B_FOLLOW_ALL, B_FRAME_EVENTS|B_WILL_DRAW) { - fFileTypeBox = new BBox(BRect(25,25,325,175),"File Type"); + SetViewColor( ui_color(B_PANEL_BACKGROUND_COLOR) ); + + const char * fileTypeLabel = "File Type"; + BRect fileTypeRect(10,10,viewFrame.Width()-55,90); + fFileTypeBox = new BBox(fileTypeRect,fileTypeLabel, + B_FOLLOW_LEFT_RIGHT|B_FOLLOW_TOP); + fFileTypeBox->SetLabel(fileTypeLabel); AddChild(fFileTypeBox); - fPreferredApplicationBox = new BBox(BRect(25,225,325,375),"Preferred Application"); - AddChild(fPreferredApplicationBox); + BRect fileTypeTextControlRect(10,18,fileTypeRect.Width()-10,fileTypeRect.Height()); + fFileTypeTextControl = new BTextControl(fileTypeTextControlRect,"mime", + NULL,NULL,NULL, + B_FOLLOW_LEFT_RIGHT|B_FOLLOW_TOP); + fFileTypeBox->AddChild(fFileTypeTextControl); + + const char * preferredAppLabel = "Preferred Application"; + BRect preferredAppRect(10,95,viewFrame.Width()-55,170); + fPreferredAppBox = new BBox(preferredAppRect,preferredAppLabel, + B_FOLLOW_LEFT_RIGHT|B_FOLLOW_BOTTOM); + fPreferredAppBox->SetLabel(preferredAppLabel); + AddChild(fPreferredAppBox); + + fPreferredAppMenu = new BMenu("app"); + fPreferredAppMenuItemNone = new BMenuItem("None",NULL); + fPreferredAppMenu->AddItem(fPreferredAppMenuItemNone); + fPreferredAppMenu->AddSeparatorItem(); + fPreferredAppMenu->SetRadioMode(true); + fPreferredAppMenu->SetLabelFromMarked(true); + fPreferredAppMenuItemNone->SetMarked(true); + + BRect preferredAppMenuFieldRect(10,15,preferredAppRect.Width()-10, + preferredAppRect.Height()); + fPreferredAppMenuField = new BMenuField(preferredAppMenuFieldRect, + "appField",NULL,fPreferredAppMenu); + fPreferredAppBox->AddChild(fPreferredAppMenuField); } FileTypeView::~FileTypeView() @@ -19,14 +51,22 @@ void FileTypeView::SetFileType(const char * fileType) { fFileType.SetTo(fileType); - // change UI + fFileTypeTextControl->SetText(fileType); } void FileTypeView::SetPreferredApplication(const char * preferredApplication) { - fPreferredApplication.SetTo(preferredApplication); - // change UI + if (preferredApplication == 0) { + fPreferredApp.SetTo(preferredApplication); + fPreferredAppMenuItemNone->SetMarked(true); + } else { + BMenuItem * item = fPreferredAppMenu->FindItem(preferredApplication); + if (item != 0) { + fPreferredApp.SetTo(preferredApplication); + item->SetMarked(true); + } + } } bool @@ -35,7 +75,7 @@ FileTypeView::IsClean() const if (fFileType.Compare(GetFileType()) != 0) { return false; } - if (fPreferredApplication.Compare(GetPreferredApplication()) != 0) { + if (fPreferredApp.Compare(GetPreferredApplication()) != 0) { return false; } return true; @@ -44,13 +84,18 @@ FileTypeView::IsClean() const const char * FileTypeView::GetFileType() const { - // return from UI - return ""; + return fFileTypeTextControl->Text(); } const char * FileTypeView::GetPreferredApplication() const { - // return from UI - return ""; + BMenuItem * item = fPreferredAppMenu->FindMarked(); + if (item == 0) { + return 0; + } + if (item == fPreferredAppMenuItemNone) { + return 0; + } + return item->Label(); } diff --git a/src/add-ons/tracker/filetype/FileTypeView.h b/src/add-ons/tracker/filetype/FileTypeView.h index fdde49002e..5c64a13c9d 100644 --- a/src/add-ons/tracker/filetype/FileTypeView.h +++ b/src/add-ons/tracker/filetype/FileTypeView.h @@ -3,8 +3,9 @@ #include #include -#include +#include #include +#include #include class FileTypeView : public BView { @@ -20,16 +21,18 @@ public: const char * GetPreferredApplication() const; private: BString fFileType; - BString fPreferredApplication; + BString fPreferredApp; - BBox * fFileTypeBox; - BTextView * fFileTypeTextView; - BButton * fFileTypeSelectButton; - BButton * fFileTypeSameAsButton; - BBox * fPreferredApplicationBox; - BPopUpMenu * fPreferredApplicationMenu; - BButton * fPreferredApplicationSelectButton; - BButton * fPreferredApplicationSameAsButton; + BBox * fFileTypeBox; + BTextControl * fFileTypeTextControl; + BButton * fFileTypeSelectButton; + BButton * fFileTypeSameAsButton; + BBox * fPreferredAppBox; + BMenu * fPreferredAppMenu; + BMenuItem * fPreferredAppMenuItemNone; + BMenuField * fPreferredAppMenuField; + BButton * fPreferredAppSelectButton; + BButton * fPreferredAppSameAsButton; }; #endif // FILE_TYPE_VIEW_H diff --git a/src/add-ons/tracker/filetype/FileTypeWindow.cpp b/src/add-ons/tracker/filetype/FileTypeWindow.cpp index 708c6feeea..0faa4a08cf 100644 --- a/src/add-ons/tracker/filetype/FileTypeWindow.cpp +++ b/src/add-ons/tracker/filetype/FileTypeWindow.cpp @@ -1,14 +1,16 @@ #include +#include #include #include #include #include +#include #include #include FileTypeWindow::FileTypeWindow(const BList * entryList) - : BWindow(BRect(250,150,350,400),"File Type",B_TITLED_WINDOW, + : BWindow(BRect(100,100,380,300),"File Type",B_TITLED_WINDOW, B_NOT_ZOOMABLE|B_NOT_RESIZABLE|B_ASYNCHRONOUS_CONTROLS) { initStatus = B_ERROR; @@ -69,6 +71,18 @@ FileTypeWindow::MessageReceived(BMessage * message) } } +void +FileTypeWindow::Quit() +{ + { + // This is in its own scope because it must be released + // before the call to BWindow::Quit() + BAutolock lock(file_type_app); + file_type_app->Quit(); + } + BWindow::Quit(); +} + bool FileTypeWindow::QuitRequested() { @@ -96,7 +110,7 @@ FileTypeWindow::QuitRequested() } else { // save errors are ignored: there's usually no good way for the user to recover return true; - } + } } status_t @@ -127,6 +141,9 @@ FileTypeWindow::SaveRequested() } } } + + fView->SetFileType(fileType); + fView->SetPreferredApplication(preferredApplication); return result; } diff --git a/src/add-ons/tracker/filetype/FileTypeWindow.h b/src/add-ons/tracker/filetype/FileTypeWindow.h index 05e15106c9..14c1a5dfcc 100644 --- a/src/add-ons/tracker/filetype/FileTypeWindow.h +++ b/src/add-ons/tracker/filetype/FileTypeWindow.h @@ -16,6 +16,7 @@ public: FileTypeWindow(const BList * entryList); ~FileTypeWindow(); + virtual void Quit(); virtual bool QuitRequested(); virtual void MessageReceived(BMessage * message);