From 478ff1324217521f6c209bc6893cff0bfa5e9919 Mon Sep 17 00:00:00 2001 From: shatty Date: Sun, 17 Aug 2003 13:53:23 +0000 Subject: [PATCH] first cut git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4292 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/prefs/filetypes/FileTypeView.cpp | 56 ++++++++ src/prefs/filetypes/FileTypeView.h | 35 +++++ src/prefs/filetypes/FileTypeWindow.cpp | 184 +++++++++++++++++++++++++ src/prefs/filetypes/FileTypeWindow.h | 36 +++++ 4 files changed, 311 insertions(+) create mode 100644 src/prefs/filetypes/FileTypeView.cpp create mode 100644 src/prefs/filetypes/FileTypeView.h create mode 100644 src/prefs/filetypes/FileTypeWindow.cpp create mode 100644 src/prefs/filetypes/FileTypeWindow.h diff --git a/src/prefs/filetypes/FileTypeView.cpp b/src/prefs/filetypes/FileTypeView.cpp new file mode 100644 index 0000000000..68ecca31f6 --- /dev/null +++ b/src/prefs/filetypes/FileTypeView.cpp @@ -0,0 +1,56 @@ +#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"); + AddChild(fFileTypeBox); + + fPreferredApplicationBox = new BBox(BRect(25,225,325,375),"Preferred Application"); + AddChild(fPreferredApplicationBox); +} + +FileTypeView::~FileTypeView() +{ +} + +void +FileTypeView::SetFileType(const char * fileType) +{ + fFileType.SetTo(fileType); + // change UI +} + +void +FileTypeView::SetPreferredApplication(const char * preferredApplication) +{ + fPreferredApplication.SetTo(preferredApplication); + // change UI +} + +bool +FileTypeView::Clean() +{ + if (fFileType.Compare(GetFileType()) != 0) { + return false; + } + if (fPreferredApplication.Compare(GetPreferredApplication()) != 0) { + return false; + } + return true; +} + +const char * +FileTypeView::GetFileType() +{ + // return from UI + return ""; +} + +const char * +FileTypeView::GetPreferredApplication() +{ + // return from UI + return ""; +} diff --git a/src/prefs/filetypes/FileTypeView.h b/src/prefs/filetypes/FileTypeView.h new file mode 100644 index 0000000000..ba6c9aee42 --- /dev/null +++ b/src/prefs/filetypes/FileTypeView.h @@ -0,0 +1,35 @@ +#ifndef FILE_TYPE_VIEW_H +#define FILE_TYPE_VIEW_H + +#include +#include +#include +#include +#include + +class FileTypeView : public BView { +public: + FileTypeView(BRect viewFrame); + ~FileTypeView(); + + void SetFileType(const char * fileType); + void SetPreferredApplication(const char * preferredApplication); + + bool Clean() const; + const char * GetFileType() const; + const char * GetPreferredApplication() const; +private: + BString fFileType; + BString fPreferredApplication; + + BBox * fFileTypeBox; + BTextView * fFileTypeTextView; + BButton * fFileTypeSelectButton; + BButton * fFileTypeSameAsButton; + BBox * fPreferredApplicationBox; + BPopUpMenu * fPreferredApplicationMenu; + BButton * fPreferredApplicationSelectButton; + BButton * fPreferredApplicationSameAsButton; +}; + +#endif // FILE_TYPE_VIEW_H diff --git a/src/prefs/filetypes/FileTypeWindow.cpp b/src/prefs/filetypes/FileTypeWindow.cpp new file mode 100644 index 0000000000..0da6b86802 --- /dev/null +++ b/src/prefs/filetypes/FileTypeWindow.cpp @@ -0,0 +1,184 @@ +#include + +#include +#include + +FileTypeWindow::FileTypeWindow(BEntryList entries) + : BWindow(BRect(250,150,350,400),"File Type",B_TITLED_WINDOW, + B_NOT_ZOOMABLE|B_NOT_RESIZABLE|B_ASYNCHRONOUS_CONTROLS) +{ + fMenuBar = new BMenuBar(BRect(0,0,0,0),"menubar"); + AddChild(fMenuBar); + + BRect viewFrame = Bounds(); + viewFrame.top = fMenuBar->Bounds().Height()+1; + fView = new FileTypeView(viewFrame); + AddChild(fView); + fView->MakeFocus(true); + + fFileMenu = new BMenu("File"); + fMenuBar->AddItem(fFileMenu); + fSaveItem = new BMenuItem("Save",new BMessage(B_SAVE_REQUESTED), 'S'); + fFileMenu->AddItem(fSaveItem); + fFileMenu->AddSeperator(); + fCloseItem = new BMenuItem("Close",new BMessage(B_QUIT_REQUESTED), 'W'); + fFileMenu->AddItem(fCloseItem); + + SetEntries(entries); + Show(); +} + +FileTypeWindow::~FileTypeWindow() +{ +} + +void +FileTypeWindow::MessageReceived(BMessage *message) +{ + switch(message->what){ + case B_SAVE_REQUESTED: + SaveRequested(); + break; + default: + BWindow::MessageReceived(message); + break; + } +} + +bool +FileTypeWindow::QuitRequested() +{ + if (fView->Clean()) { + return true; + } + + BAlert * saveAlert; + BString alertText("Would you like to save changes to file type attributes of "); + alertText << SummarizeEntries(); + alertText << "? "; + saveAlert = new BAlert("savealert",alertText.String(), "Cancel", "Don't Save","Save", + B_WIDTH_AS_USUAL, B_OFFSET_SPACING, B_WARNING_ALERT); + saveAlert->SetShortcut(0, B_ESCAPE); + saveAlert->SetShortcut(1,'d'); + saveAlert->SetShortcut(2,'s'); + buttonIndex = saveAlert->Go(); + + if (buttonIndex==0) { //"cancel": dont save, dont close the window + return false; + } else if (buttonIndex==1) { // "don't save": just close the window + return true; + } else if (SaveRequested() == B_OK) { + return true; + } else { + // save errors are ignored: there's usually no good way for the user to recover + return true; + } +} + +status_t +FileTypeWindow::SaveRequested(BMessage * message) +{ + status_t result = B_OK; + + const char * fileType = fView->GetFileType(); + const char * preferredApplication = fView->GetPreferredApplication(); + + if ((result = fEntries->Rewind()) != B_OK) { + return result; // can't proceed with no entries + } + BEntry entry; + while (fEntries->GetNextEntry(&entry,true) != B_OK) { + BNode node(&entry); + if ((result = node.InitCheck()) != B_OK) { + // save errors are ignored: there's usually no good way for the user to recover + continue; // can't proceed with an invalid node + } + BNodeInfo nodeInfo(&node); + if ((result = nodeInfo.InitCheck()) != B_OK) { + // save errors are ignored: there's usually no good way for the user to recover + continue; // can't proceed with an invalid nodeinfo + } + if ((result = nodeInfo.SetType(fileType)) != B_OK) { + // save errors are ignored: there's usually no good way for the user to recover + } + if ((result = nodeInfo.SetPreferredApp(preferredApplication)) != B_OK) { + // save errors are ignored: there's usually no good way for the user to recover + } + } + return result; +} + +void +FileTypeWindow::SetEntries(BEntryList entries) +{ + fEntries = entries; + + BString title = SummarizeEntries(); + title.Append(" File Type"); + SetTitle(title); + + if (fEntries->Rewind() != B_OK) { + return; + } + BString * fileType = 0; + BString * preferredApplication = 0; + BEntry entry; + while (fEntries->GetNextEntry(&entry,true) != B_OK) { + BNode node(&entry); + if ((result = node.InitCheck()) != B_OK) { + // errors are ignored: there's usually no good way for the user to recover + continue; // can't proceed with an invalid node + } + BNodeInfo nodeInfo(&node); + if ((result = nodeInfo.InitCheck()) != B_OK) { + // errors are ignored: there's usually no good way for the user to recover + continue; // can't proceed with an invalid nodeinfo + } + char string[MAXPATHLEN]; + if ((result = nodeInfo.GetType(string)) != B_OK) { + // errors are ignored: there's usually no good way for the user to recover + } else { + if (fileType == 0) { + fileType = new BString(string); + } else if (fileType->Compare(string) != 0) { + fileType.SetTo(""); + if (preferredApplication && (preferredApplication->Length() == 0)) { + break; // stop now, don't waste time checking the rest + } + } + } + if ((result = nodeInfo.GetPreferredApp(string)) != B_OK) { + // errors are ignored: there's usually no good way for the user to recover + } else { + if (preferredApplication == 0) { + preferredApplication = new BString(string); + } else if (preferredApplication->Compare(string) != 0) { + preferredApplication.SetTo(""); + if (fileType && (fileType->Length() == 0)) { + break; // stop now, don't waste time checking the rest + } + } + } + } + fView->SetFileType(fileType.String()); + fView->SetPreferredApplication(preferredApplication.String()); + delete fileType; + delete preferredApplication; +} + +const char * +FileTypeWindow::SummarizeEntries() +{ + if (fEntries->Rewind() != B_OK) { + return ""; + } + const char * result = ""; + dirent dent; + if (fEntries->GetNextDirents(&dent,sizeof(dirent),1) != 0) { + result = strdup(dent.d_name); + if (fEntries->GetNextDirents(&dent,sizeof(dirent),1) != 0) { + result = "[Multiple Files]"; + } + } + return result; +} diff --git a/src/prefs/filetypes/FileTypeWindow.h b/src/prefs/filetypes/FileTypeWindow.h new file mode 100644 index 0000000000..8ee4ef3445 --- /dev/null +++ b/src/prefs/filetypes/FileTypeWindow.h @@ -0,0 +1,36 @@ +#ifndef FILE_TYPE_WINDOW_H +#define FILE_TYPE_WINDOW_H + +#include +#include +#include +#include + +class FileTypeView; + +class FileTypeWindow + : public BWindow +{ +public: + FileTypeWindow(BEntryList entries); + ~FileTypeWindow(); + + virtual bool QuitRequested(); + virtual void MessageReceived(BMessage * message); + +private: + status_t SaveRequested(); + void SetEntries(BEntryList entries); + const char * SummarizeEntries(); + + BMenuBar *fMenuBar; + BMenu *fFileMenu; + BMenuItem *fSaveItem; + BMenuItem *fCloseItem; + + FileTypeView *fView; + + BEntryList fEntries; +}; + +#endif // FILE_TYPE_WINDOW_H