From f4e8263700211f15194530da5a5e255dd76f2029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Thu, 1 Jun 2006 11:43:18 +0000 Subject: [PATCH] * FileTypes now stores and retrieves its settings. * The "Show Icons" option now defaults to true. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@17689 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../filetypes/ApplicationTypesWindow.cpp | 19 ++- .../filetypes/ApplicationTypesWindow.h | 3 +- src/preferences/filetypes/FileTypes.cpp | 124 ++++++++++++++++-- src/preferences/filetypes/FileTypes.h | 2 + src/preferences/filetypes/FileTypesWindow.cpp | 42 +++++- src/preferences/filetypes/FileTypesWindow.h | 3 +- 6 files changed, 176 insertions(+), 17 deletions(-) diff --git a/src/preferences/filetypes/ApplicationTypesWindow.cpp b/src/preferences/filetypes/ApplicationTypesWindow.cpp index 5cd1bf83de..5e30b1b86c 100644 --- a/src/preferences/filetypes/ApplicationTypesWindow.cpp +++ b/src/preferences/filetypes/ApplicationTypesWindow.cpp @@ -158,8 +158,8 @@ ProgressWindow::MessageReceived(BMessage* message) // #pragma mark - -ApplicationTypesWindow::ApplicationTypesWindow(BRect frame) - : BWindow(frame, "Application Types", B_TITLED_WINDOW, +ApplicationTypesWindow::ApplicationTypesWindow(const BMessage &settings) + : BWindow(_Frame(settings), "Application Types", B_TITLED_WINDOW, B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS) { // Application list @@ -295,6 +295,17 @@ ApplicationTypesWindow::~ApplicationTypesWindow() } +BRect +ApplicationTypesWindow::_Frame(const BMessage& settings) const +{ + BRect rect; + if (settings.FindRect("app_types_frame", &rect) == B_OK) + return rect; + + return BRect(100.0f, 100.0f, 540.0f, 480.0f); +} + + void ApplicationTypesWindow::_RemoveUninstalled() { @@ -542,6 +553,10 @@ ApplicationTypesWindow::MessageReceived(BMessage* message) bool ApplicationTypesWindow::QuitRequested() { + BMessage update(kMsgSettingsChanged); + update.AddRect("app_types_frame", Frame()); + be_app_messenger.SendMessage(&update); + be_app->PostMessage(kMsgApplicationTypesWindowClosed); return true; } diff --git a/src/preferences/filetypes/ApplicationTypesWindow.h b/src/preferences/filetypes/ApplicationTypesWindow.h index 10b1e96a2a..83e4feefb8 100644 --- a/src/preferences/filetypes/ApplicationTypesWindow.h +++ b/src/preferences/filetypes/ApplicationTypesWindow.h @@ -24,7 +24,7 @@ class StringView; class ApplicationTypesWindow : public BWindow { public: - ApplicationTypesWindow(BRect frame); + ApplicationTypesWindow(const BMessage& settings); virtual ~ApplicationTypesWindow(); virtual void FrameResized(float width, float height); @@ -32,6 +32,7 @@ class ApplicationTypesWindow : public BWindow { virtual bool QuitRequested(); private: + BRect _Frame(const BMessage& settings) const; void _SetType(BMimeType* type, int32 forceUpdate = 0); void _UpdateCounter(); void _RemoveUninstalled(); diff --git a/src/preferences/filetypes/FileTypes.cpp b/src/preferences/filetypes/FileTypes.cpp index 8c06d1fb0e..edcdefe4d3 100644 --- a/src/preferences/filetypes/FileTypes.cpp +++ b/src/preferences/filetypes/FileTypes.cpp @@ -26,6 +26,26 @@ const char *kSignature = "application/x-vnd.Haiku-FileTypes"; +static const uint32 kMsgFileTypesSettings = 'FTst'; +static const uint32 kCascadeOffset = 20; + + +class Settings { + public: + Settings(); + ~Settings(); + + const BMessage &Message() const { return fMessage; } + void UpdateFrom(BMessage *message); + + private: + void _SetDefaults(); + status_t _Open(BFile *file, int32 mode); + + BMessage fMessage; + bool fUpdated; +}; + class FileTypes : public BApplication { public: FileTypes(); @@ -43,17 +63,99 @@ class FileTypes : public BApplication { private: void _WindowClosed(); + Settings fSettings; BFilePanel *fFilePanel; BMessenger fFilePanelTarget; BWindow *fTypesWindow; BWindow *fApplicationTypesWindow; uint32 fWindowCount; uint32 fTypeWindowCount; - BRect fTypesWindowFrame; - BRect fApplicationTypesWindowFrame; }; +Settings::Settings() + : + fMessage(kMsgFileTypesSettings), + fUpdated(false) +{ + _SetDefaults(); + + BFile file; + if (_Open(&file, B_READ_ONLY) != B_OK) + return; + + BMessage settings; + if (settings.Unflatten(&file) == B_OK) { + // We don't unflatten into our default message to make sure + // nothing is lost (because of old or corrupted on disk settings) + UpdateFrom(&settings); + fUpdated = false; + } +} + + +Settings::~Settings() +{ + // only save the settings if something has changed + if (!fUpdated) + return; + + BFile file; + if (_Open(&file, B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY) != B_OK) + return; + + fMessage.Flatten(&file); +} + + +void +Settings::_SetDefaults() +{ + fMessage.AddRect("file_types_frame", BRect(80.0f, 80.0f, 600.0f, 480.0f)); + fMessage.AddRect("app_types_frame", BRect(100.0f, 100.0f, 540.0f, 480.0f)); + fMessage.AddBool("show_icons", true); + fMessage.AddBool("show_rule", false); +} + + +status_t +Settings::_Open(BFile *file, int32 mode) +{ + BPath path; + if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) + return B_ERROR; + + path.Append("FileTypes settings"); + + return file->SetTo(path.Path(), mode); +} + + +void +Settings::UpdateFrom(BMessage *message) +{ + BRect frame; + if (message->FindRect("file_types_frame", &frame) == B_OK) + fMessage.ReplaceRect("file_types_frame", frame); + + if (message->FindRect("app_types_frame", &frame) == B_OK) + fMessage.ReplaceRect("app_types_frame", frame); + + bool showIcons; + if (message->FindBool("show_icons", &showIcons) == B_OK) + fMessage.ReplaceBool("show_icons", showIcons); + + bool showRule; + if (message->FindBool("show_rule", &showRule) == B_OK) + fMessage.ReplaceBool("show_rule", showRule); + + fUpdated = true; +} + + +// #pragma mark - + + FileTypes::FileTypes() : BApplication(kSignature), fTypesWindow(NULL), @@ -63,10 +165,6 @@ FileTypes::FileTypes() { fFilePanel = new BFilePanel(B_OPEN_PANEL, NULL, NULL, B_FILE_NODE | B_DIRECTORY_NODE, false); - - fTypesWindowFrame = BRect(80.0f, 80.0f, 600.0f, 480.0f); - fApplicationTypesWindowFrame = BRect(100.0f, 100.0f, 540.0f, 480.0f); - // TODO: read from settings } @@ -131,7 +229,8 @@ FileTypes::RefsReceived(BMessage *message) message->RemoveData("refs", --index); // There are some refs left that want to be handled by the type window - BPoint point(100.0f + 20.0f * fTypeWindowCount, 110.0f + 20.0f * fTypeWindowCount); + BPoint point(100.0f + kCascadeOffset * fTypeWindowCount, + 110.0f + kCascadeOffset * fTypeWindowCount); BWindow* window = new ApplicationTypeWindow(point, entry); window->Show(); @@ -144,7 +243,8 @@ FileTypes::RefsReceived(BMessage *message) return; // There are some refs left that want to be handled by the type window - BPoint point(100.0f + 20.0f * fTypeWindowCount, 110.0f + 20.0f * fTypeWindowCount); + BPoint point(100.0f + kCascadeOffset * fTypeWindowCount, + 110.0f + kCascadeOffset * fTypeWindowCount); BWindow* window = new FileTypeWindow(point, *message); window->Show(); @@ -202,9 +302,13 @@ void FileTypes::MessageReceived(BMessage *message) { switch (message->what) { + case kMsgSettingsChanged: + fSettings.UpdateFrom(message); + break; + case kMsgOpenTypesWindow: if (fTypesWindow == NULL) { - fTypesWindow = new FileTypesWindow(fTypesWindowFrame); + fTypesWindow = new FileTypesWindow(fSettings.Message()); fTypesWindow->Show(); fWindowCount++; } else @@ -218,7 +322,7 @@ FileTypes::MessageReceived(BMessage *message) case kMsgOpenApplicationTypesWindow: if (fApplicationTypesWindow == NULL) { fApplicationTypesWindow = new ApplicationTypesWindow( - fApplicationTypesWindowFrame); + fSettings.Message()); fApplicationTypesWindow->Show(); fWindowCount++; } else diff --git a/src/preferences/filetypes/FileTypes.h b/src/preferences/filetypes/FileTypes.h index 9c0ddadc1b..c7268340c8 100644 --- a/src/preferences/filetypes/FileTypes.h +++ b/src/preferences/filetypes/FileTypes.h @@ -24,6 +24,8 @@ static const uint32 kMsgApplicationTypesWindowClosed = 'clAw'; static const uint32 kMsgTypeWindowClosed = 'cltw'; static const uint32 kMsgWindowClosed = 'WiCl'; +static const uint32 kMsgSettingsChanged = 'SeCh'; + // exported functions diff --git a/src/preferences/filetypes/FileTypesWindow.cpp b/src/preferences/filetypes/FileTypesWindow.cpp index 4c1d13550c..30dc78839d 100644 --- a/src/preferences/filetypes/FileTypesWindow.cpp +++ b/src/preferences/filetypes/FileTypesWindow.cpp @@ -232,11 +232,18 @@ TypeIconView::MouseDown(BPoint where) // #pragma mark - -FileTypesWindow::FileTypesWindow(BRect frame) - : BWindow(frame, "FileTypes", B_TITLED_WINDOW, +FileTypesWindow::FileTypesWindow(const BMessage& settings) + : BWindow(_Frame(settings), "FileTypes", B_TITLED_WINDOW, B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS), fNewTypeWindow(NULL) { + bool showIcons; + bool showRule; + if (settings.FindBool("show_icons", &showIcons) != B_OK) + showIcons = true; + if (settings.FindBool("show_rule", &showRule) != B_OK) + showRule = false; + // add the menu BMenuBar* menuBar = new BMenuBar(BRect(0, 0, 0, 0), NULL); @@ -268,10 +275,12 @@ FileTypesWindow::FileTypesWindow(BRect frame) menu = new BMenu("Settings"); item = new BMenuItem("Show Icons in List", new BMessage(kMsgToggleIcons)); + item->SetMarked(showIcons); item->SetTarget(this); menu->AddItem(item); item = new BMenuItem("Show Recognition Rule", new BMessage(kMsgToggleRule)); + item->SetMarked(showRule); item->SetTarget(this); menu->AddItem(item); menuBar->AddItem(menu); @@ -304,7 +313,7 @@ FileTypesWindow::FileTypesWindow(BRect frame) if (rect.right < 180) rect.right = 180; - fTypeListView = new MimeTypeListView(rect, "typeview", NULL, false, false, + fTypeListView = new MimeTypeListView(rect, "typeview", NULL, showIcons, false, B_FOLLOW_LEFT | B_FOLLOW_TOP_BOTTOM); fTypeListView->SetSelectionMessage(new BMessage(kMsgTypeSelected)); @@ -527,6 +536,8 @@ FileTypesWindow::FileTypesWindow(BRect frame) + 32.0f + menuBar->Bounds().Height(), 32767.0f); _SetType(NULL); + _ShowSnifferRule(showRule); + BMimeType::StartWatching(this); } @@ -537,6 +548,17 @@ FileTypesWindow::~FileTypesWindow() } +BRect +FileTypesWindow::_Frame(const BMessage& settings) const +{ + BRect rect; + if (settings.FindRect("file_types_frame", &rect) == B_OK) + return rect; + + return BRect(80.0f, 80.0f, 600.0f, 480.0f); +} + + void FileTypesWindow::_ShowSnifferRule(bool show) { @@ -741,6 +763,11 @@ FileTypesWindow::MessageReceived(BMessage* message) item->SetMarked(!fTypeListView->IsShowingIcons()); fTypeListView->ShowIcons(item->IsMarked()); + + // update settings + BMessage update(kMsgSettingsChanged); + update.AddBool("show_icons", item->IsMarked()); + be_app_messenger.SendMessage(&update); break; } @@ -752,6 +779,11 @@ FileTypesWindow::MessageReceived(BMessage* message) item->SetMarked(fRuleControl->IsHidden()); _ShowSnifferRule(item->IsMarked()); + + // update settings + BMessage update(kMsgSettingsChanged); + update.AddBool("show_rule", item->IsMarked()); + be_app_messenger.SendMessage(&update); break; } @@ -1044,6 +1076,10 @@ FileTypesWindow::MessageReceived(BMessage* message) bool FileTypesWindow::QuitRequested() { + BMessage update(kMsgSettingsChanged); + update.AddRect("file_types_frame", Frame()); + be_app_messenger.SendMessage(&update); + be_app->PostMessage(kMsgTypesWindowClosed); return true; } diff --git a/src/preferences/filetypes/FileTypesWindow.h b/src/preferences/filetypes/FileTypesWindow.h index 8ba7eaa2bd..148e646de1 100644 --- a/src/preferences/filetypes/FileTypesWindow.h +++ b/src/preferences/filetypes/FileTypesWindow.h @@ -26,7 +26,7 @@ class StringView; class FileTypesWindow : public BWindow { public: - FileTypesWindow(BRect frame); + FileTypesWindow(const BMessage& settings); virtual ~FileTypesWindow(); virtual void MessageReceived(BMessage* message); @@ -35,6 +35,7 @@ class FileTypesWindow : public BWindow { void PlaceSubWindow(BWindow* window); private: + BRect _Frame(const BMessage& settings) const; void _ShowSnifferRule(bool show); void _UpdateExtensions(BMimeType* type); void _AdoptPreferredApplication(BMessage* message, bool sameAs);