1 /*
2 * Copyright 2002-2006, Haiku. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 * Michael Pfeiffer
7 */
8 #include "Settings.h"
9
10 #include <StorageKit.h>
11
12
13 // Implementation of AppSettings
14
AppSettings(const char * mimetype,const char * printer)15 AppSettings::AppSettings(const char* mimetype, const char* printer)
16 : fMimeType(mimetype)
17 {
18 if (printer != NULL)
19 fPrinter = printer;
20 }
21
22
23 // Implementation of PrinterSettings
24
PrinterSettings(const char * printer,BMessage * pageSettings,BMessage * jobSettings)25 PrinterSettings::PrinterSettings(const char* printer,
26 BMessage* pageSettings, BMessage* jobSettings)
27 : fPrinter(printer)
28 {
29 if (pageSettings != NULL)
30 fPageSettings = *pageSettings;
31 if (jobSettings != NULL)
32 fJobSettings = *jobSettings;
33 }
34
35
36 // Implementation of Settings
37
38 Settings* Settings::sSingleton = NULL;
39
40 static const BRect kConfigWindowFrame(30, 30, 220, 120);
41
Settings()42 Settings::Settings()
43 : fApps(true) // owns AppSettings
44 , fPrinters(true) // owns PrinterSettings
45 , fUseConfigWindow(true)
46 , fConfigWindowFrame(kConfigWindowFrame)
47 {
48 }
49
50
~Settings()51 Settings::~Settings()
52 {
53 sSingleton = NULL;
54 }
55
56
57 Settings*
GetSettings()58 Settings::GetSettings()
59 {
60 if (sSingleton == NULL)
61 sSingleton = new Settings();
62 return sSingleton;
63 }
64
65
66 void
RemoveAppSettings(int i)67 Settings::RemoveAppSettings(int i)
68 {
69 delete fApps.RemoveItemAt(i);
70 }
71
72
73 void
RemovePrinterSettings(int i)74 Settings::RemovePrinterSettings(int i)
75 {
76 delete fPrinters.RemoveItemAt(i);
77 }
78
79
80 AppSettings*
FindAppSettings(const char * mimeType)81 Settings::FindAppSettings(const char* mimeType)
82 {
83 for (int i = AppSettingsCount() - 1; i >= 0; i --) {
84 if (strcmp(AppSettingsAt(i)->GetMimeType(), mimeType) == 0)
85 return AppSettingsAt(i);
86 }
87 return NULL;
88 }
89
90
91 PrinterSettings*
FindPrinterSettings(const char * printer)92 Settings::FindPrinterSettings(const char* printer)
93 {
94 for (int i = PrinterSettingsCount() - 1; i >= 0; i --) {
95 if (strcmp(PrinterSettingsAt(i)->GetPrinter(), printer) == 0)
96 return PrinterSettingsAt(i);
97 }
98 return NULL;
99 }
100
101
102 void
Save(BFile * file)103 Settings::Save(BFile* file)
104 {
105 BMessage message;
106 // store application settings
107 for (int i = 0; i < AppSettingsCount(); i++) {
108 AppSettings* app = AppSettingsAt(i);
109 message.AddString("message", app->GetMimeType());
110 message.AddString("p", app->GetPrinter());
111 }
112 // store printer settings
113 for (int i = 0; i < PrinterSettingsCount(); i++) {
114 PrinterSettings* p = PrinterSettingsAt(i);
115 message.AddString("P", p->GetPrinter());
116 message.AddMessage("S", p->GetPageSettings());
117 message.AddMessage("J", p->GetJobSettings());
118 }
119
120 message.AddBool("UseConfigWindow", fUseConfigWindow);
121 message.AddRect("ConfigWindowFrame", fConfigWindowFrame);
122 message.AddString("DefaultPrinter", fDefaultPrinter);
123 message.Flatten(file);
124 }
125
126
127 void
Load(BFile * file)128 Settings::Load(BFile* file)
129 {
130 BMessage message;
131 if (message.Unflatten(file) == B_OK) {
132 // restore application settings
133 BString mimetype;
134 BString printer;
135 for (int i = 0; message.FindString("message", i, &mimetype) == B_OK &&
136 message.FindString("p", i, &printer ) == B_OK; i ++) {
137 AddAppSettings(new AppSettings(mimetype.String(), printer.String()));
138 }
139
140 // restore printer settings
141 BMessage page;
142 BMessage job;
143 for (int i = 0; message.FindString("P", i, &printer) == B_OK &&
144 message.FindMessage("S", i, &page) == B_OK &&
145 message.FindMessage("J", i, &job) == B_OK; i ++) {
146 AddPrinterSettings(new PrinterSettings(printer.String(), &page, &job));
147 }
148
149 if (message.FindBool("UseConfigWindow", &fUseConfigWindow) != B_OK)
150 fUseConfigWindow = true;
151
152 if (message.FindRect("ConfigWindowFrame", &fConfigWindowFrame) != B_OK)
153 fConfigWindowFrame = BRect(kConfigWindowFrame);
154
155 if (message.FindString("DefaultPrinter", &fDefaultPrinter) != B_OK)
156 fDefaultPrinter = "";
157 }
158 }
159