Bugfixes to saving and loading colorsets

Added some safeguards to prevent saving sets with the (now) reserved name 'Default'


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4978 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
DarkWyrm
2003-10-08 00:22:18 +00:00
parent e735d45a6a
commit 7e0de72b59
3 changed files with 89 additions and 43 deletions
+87 -42
View File
@@ -39,10 +39,12 @@
#include "APRView.h" #include "APRView.h"
#include <PortLink.h> #include <PortLink.h>
#include <PortMessage.h>
#include "defs.h" #include "defs.h"
#include "ColorWell.h" #include "ColorWell.h"
#include <ColorUtils.h> #include <ColorUtils.h>
#include <InterfaceDefs.h> #include <InterfaceDefs.h>
#include <ServerProtocol.h>
#include "ColorWhichItem.h" #include "ColorWhichItem.h"
#include "ServerConfig.h" #include "ServerConfig.h"
@@ -171,7 +173,7 @@ APRView::APRView(const BRect &frame, const char *name, int32 resize, int32 flags
savepanel=new BFilePanel(B_SAVE_PANEL, NULL, &ref, 0, false); savepanel=new BFilePanel(B_SAVE_PANEL, NULL, &ref, 0, false);
attribute=B_PANEL_BACKGROUND_COLOR; attribute=B_PANEL_BACKGROUND_COLOR;
attrstring="Background"; attrstring="Panel Background";
LoadSettings(); LoadSettings();
} }
@@ -211,6 +213,7 @@ void APRView::MessageReceived(BMessage *msg)
{ {
picker->SetValue(*col); picker->SetValue(*col);
colorwell->SetColor(*col); colorwell->SetColor(*col);
colorwell->Invalidate();
} }
} }
@@ -218,6 +221,10 @@ void APRView::MessageReceived(BMessage *msg)
{ {
case DELETE_COLORSET: case DELETE_COLORSET:
{ {
// We can't delete the Default set
if(currentset->name.Compare("Default")==0)
break;
// Construct the path and delete // Construct the path and delete
BString path(COLOR_SET_DIR); BString path(COLOR_SET_DIR);
path+=currentset->name; path+=currentset->name;
@@ -258,6 +265,10 @@ void APRView::MessageReceived(BMessage *msg)
} }
case SAVE_COLORSET: case SAVE_COLORSET:
{ {
// Saving the Default set is rather dumb
if(currentset->name.Compare("Default")==0)
break;
savepanel->Show(); savepanel->Show();
break; break;
} }
@@ -269,6 +280,13 @@ void APRView::MessageReceived(BMessage *msg)
STRACE(("MSG: Save Request - couldn't find file name\n")); STRACE(("MSG: Save Request - couldn't find file name\n"));
break; break;
} }
if(name.ICompare("Default")==0)
{
BAlert *a=new BAlert("OpenBeOS","The name 'Default' is reserved. Please choose another.","OK");
a->Go();
savepanel->Show();
break;
}
SaveColorSet(name); SaveColorSet(name);
break; break;
} }
@@ -310,10 +328,7 @@ void APRView::MessageReceived(BMessage *msg)
if(!whichitem) if(!whichitem)
break; break;
attrstring=whichitem->Text(); attrstring=whichitem->Text();
rgb_color col=currentset->StringToColor(whichitem->Text()).GetColor32(); UpdateControlsFromAttr(whichitem->Text());
picker->SetValue(col);
colorwell->SetColor(col);
colorwell->Invalidate();
break; break;
} }
case APPLY_SETTINGS: case APPLY_SETTINGS:
@@ -375,10 +390,7 @@ void APRView::MessageReceived(BMessage *msg)
apply->SetEnabled(true); apply->SetEnabled(true);
} }
SetDefaults(); SetDefaults();
rgb_color col=currentset->StringToColor(attrstring.String()).GetColor32(); UpdateControlsFromAttr(attrstring.String());
picker->SetValue(col);
colorwell->SetColor(col);
colorwell->Invalidate();
if(Window()) if(Window())
Window()->PostMessage(SET_UI_COLORS); Window()->PostMessage(SET_UI_COLORS);
break; break;
@@ -518,8 +530,7 @@ void APRView::LoadColorSet(const BString &name)
currentset->ConvertFromMessage(&settings); currentset->ConvertFromMessage(&settings);
SetColorSetName(currentset->name.String()); SetColorSetName(currentset->name.String());
picker->SetValue(currentset->StringToColor(attrstring.String()).GetColor32()); UpdateControlsFromAttr(attrstring.String());
colorwell->SetColor(picker->ValueAsColor());
} }
void APRView::SaveColorSet(const BString &name) void APRView::SaveColorSet(const BString &name)
@@ -543,7 +554,9 @@ void APRView::SaveColorSet(const BString &name)
} }
BMessage settings; BMessage settings;
currentset->name=name;
currentset->ConvertToMessage(&settings); currentset->ConvertToMessage(&settings);
if(settings.Flatten(&file)!=B_OK) if(settings.Flatten(&file)!=B_OK)
{ {
STRACE(("SaveColorSet: Couldn't flatten settings to file\n")); STRACE(("SaveColorSet: Couldn't flatten settings to file\n"));
@@ -558,6 +571,11 @@ void APRView::SaveColorSet(const BString &name)
STRACE(("SaveColorSet: Error in adding item to menu\n")); STRACE(("SaveColorSet: Error in adding item to menu\n"));
} }
SetColorSetName(name.String()); SetColorSetName(name.String());
// If we saved the color set after Applying it, the name won't be updated on disk, so
// save again to disk if this has happened.
if(prevset==NULL)
SaveSettings();
} }
void APRView::SetColorSetName(const char *name) void APRView::SetColorSetName(const char *name)
@@ -595,46 +613,62 @@ void APRView::LoadSettings(void)
// Load the current GUI color settings from disk. This is done instead of // Load the current GUI color settings from disk. This is done instead of
// getting them from the server at this point for testing purposes. // getting them from the server at this point for testing purposes.
// TODO: Add app_server UI color query code /* TODO: Uncomment the following disabled code when the app_server will handle the message
STRACE(("Loading settings from disk\n"));
BDirectory dir,newdir; // Query the server for the current settings
if(dir.SetTo(SERVER_SETTINGS_DIR)==B_ENTRY_NOT_FOUND) port_id port=find_port(SERVER_PORT_NAME);
if(port!=B_NAME_NOT_FOUND)
{ {
STRACE(("Color set folder not found. Creating %s\n",SERVER_SETTINGS_DIR)); STRACE(("Retrieving settings from app_server\n"));
create_directory(SERVER_SETTINGS_DIR,0777); PortLink link(port);
} PortMessage pmsg;
BString path(SERVER_SETTINGS_DIR);
path+=COLOR_SETTINGS_NAME;
BFile file(path.String(),B_READ_ONLY);
if(file.InitCheck()!=B_OK)
{
STRACE(("Couldn't open file %s for read\n",path.String()));
SetDefaults();
SaveSettings();
return;
}
BMessage settings;
if(settings.Unflatten(&file)!=B_OK)
{
STRACE(("Error unflattening SystemColors file %s\n",path.String()));
SetDefaults(); link.SetOpCode(AS_GET_UI_COLORS);
SaveSettings(); link.FlushWithReply(&pmsg);
pmsg.Read<ColorSet>(currentset);
} }
else
{
*/
STRACE(("Loading settings from disk\n"));
BDirectory dir,newdir;
if(dir.SetTo(SERVER_SETTINGS_DIR)==B_ENTRY_NOT_FOUND)
{
STRACE(("Color set folder not found. Creating %s\n",SERVER_SETTINGS_DIR));
create_directory(SERVER_SETTINGS_DIR,0777);
}
BString path(SERVER_SETTINGS_DIR);
path+=COLOR_SETTINGS_NAME;
BFile file(path.String(),B_READ_ONLY);
if(file.InitCheck()!=B_OK)
{
STRACE(("Couldn't open file %s for read\n",path.String()));
SetDefaults();
SaveSettings();
return;
}
BMessage settings;
if(settings.Unflatten(&file)!=B_OK)
{
STRACE(("Error unflattening SystemColors file %s\n",path.String()));
SetDefaults();
SaveSettings();
}
currentset->ConvertFromMessage(&settings);
// }
currentset->ConvertFromMessage(&settings);
SetColorSetName(currentset->name.String()); SetColorSetName(currentset->name.String());
picker->SetValue(currentset->StringToColor(attrstring.String()).GetColor32()); UpdateControlsFromAttr(attrstring.String());
colorwell->SetColor(picker->ValueAsColor());
if(currentset->name.String()!="Default") if(currentset->name.Compare("Default")!=0)
defaults->SetEnabled(true); defaults->SetEnabled(true);
} }
void APRView::SetDefaults(void) void APRView::SetDefaults(void)
@@ -677,3 +711,14 @@ void APRView::NotifyServer(void)
if(Window()) if(Window())
Window()->PostMessage(SET_UI_COLORS); Window()->PostMessage(SET_UI_COLORS);
} }
void APRView::UpdateControlsFromAttr(const char *string)
{
if(!string)
return;
STRACE(("Update color for %s\n",string));
picker->SetValue(currentset->StringToColor(string).GetColor32());
colorwell->SetColor(picker->ValueAsColor());
colorwell->Invalidate();
}
+1
View File
@@ -60,6 +60,7 @@ public:
// rgb_color GetColorFromMessage(BMessage *msg, const char *name, int32 index=0); // rgb_color GetColorFromMessage(BMessage *msg, const char *name, int32 index=0);
protected: protected:
friend APRWindow; friend APRWindow;
void UpdateControlsFromAttr(const char *string);
BMenu *LoadColorSets(void); BMenu *LoadColorSets(void);
void SaveColorSet(const BString &name); void SaveColorSet(const BString &name);
void LoadColorSet(const BString &name); void LoadColorSet(const BString &name);
+1 -1
View File
@@ -46,7 +46,7 @@ void ColorWhichItem::SetAttribute(color_which which)
case B_PANEL_BACKGROUND_COLOR: case B_PANEL_BACKGROUND_COLOR:
{ {
attribute=which; attribute=which;
SetText("Background"); SetText("Panel Background");
break; break;
} }
case B_PANEL_TEXT_COLOR: case B_PANEL_TEXT_COLOR: