xref: /haiku/src/servers/print/Settings.cpp (revision d5cd5d63ff0ad395989db6cf4841a64d5b545d1d)
1 /*****************************************************************************/
2 // Settings
3 //
4 // Author
5 //   Michael Pfeiffer
6 //
7 // This application and all source files used in its construction, except
8 // where noted, are licensed under the MIT License, and have been written
9 // and are:
10 //
11 // Copyright (c) 2002 OpenBeOS Project
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 /*****************************************************************************/
31 
32 
33 #include "Settings.h"
34 
35 #include <StorageKit.h>
36 
37 // Implementation of AppSettings
38 
39 AppSettings::AppSettings(const char* mimetype, const char* printer)
40 	: fMimeType(mimetype)
41 {
42 	if (printer) fPrinter = printer;
43 }
44 
45 
46 // Implementation of PrinterSettings
47 
48 PrinterSettings::PrinterSettings(const char* printer, BMessage* pageSettings, BMessage* jobSettings)
49 	: fPrinter(printer)
50 {
51 	if (pageSettings) fPageSettings = *pageSettings;
52 	if (jobSettings) fJobSettings = *jobSettings;
53 }
54 
55 
56 // Implementation of Settings
57 
58 Settings* Settings::fSingleton = NULL;
59 
60 static const BRect kConfigWindowFrame(30, 30, 220, 120);
61 
62 Settings::Settings()
63 	: fApps(true)     // owns AppSettings
64 	, fPrinters(true) // owns PrinterSettings
65 	, fUseConfigWindow(true)
66 	, fConfigWindowFrame(kConfigWindowFrame)
67 {
68 }
69 
70 Settings::~Settings() {
71 	fSingleton = NULL;
72 }
73 
74 Settings* Settings::GetSettings() {
75 	if (fSingleton == NULL) fSingleton = new Settings();
76 	return fSingleton;
77 }
78 
79 void Settings::RemoveAppSettings(int i) {
80 	delete fApps.RemoveItemAt(i);
81 }
82 
83 void Settings::RemovePrinterSettings(int i) {
84 	delete fPrinters.RemoveItemAt(i);
85 }
86 
87 AppSettings* Settings::FindAppSettings(const char* mimeType) {
88 	for (int i = AppSettingsCount()-1; i >= 0; i --) {
89 		if (strcmp(AppSettingsAt(i)->GetMimeType(), mimeType) == 0)
90 			return AppSettingsAt(i);
91 	}
92 	return NULL;
93 }
94 
95 PrinterSettings* Settings::FindPrinterSettings(const char* printer) {
96 	for (int i = PrinterSettingsCount()-1; i >= 0; i --) {
97 		if (strcmp(PrinterSettingsAt(i)->GetPrinter(), printer) == 0)
98 			return PrinterSettingsAt(i);
99 	}
100 	return NULL;
101 }
102 
103 void Settings::Save(BFile* file) {
104 	BMessage m;
105 		// store application settings
106 	for (int i = 0; i < AppSettingsCount(); i++) {
107 		AppSettings* app = AppSettingsAt(i);
108 		m.AddString("m", app->GetMimeType());
109 		m.AddString("p", app->GetPrinter());
110 	}
111 		// store printer settings
112 	for (int i = 0; i < PrinterSettingsCount(); i++) {
113 		PrinterSettings* p = PrinterSettingsAt(i);
114 		m.AddString("P", p->GetPrinter());
115 		m.AddMessage("S", p->GetPageSettings());
116 		m.AddMessage("J", p->GetJobSettings());
117 	}
118 
119 	m.AddBool("UseConfigWindow", fUseConfigWindow);
120 	m.AddRect("ConfigWindowFrame", fConfigWindowFrame);
121 	m.AddString("DefaultPrinter", fDefaultPrinter);
122 	m.Flatten(file);
123 }
124 
125 void Settings::Load(BFile* file) {
126 	BMessage m;
127 	if (m.Unflatten(file) == B_OK) {
128 			// restore application settings
129 		BString mimetype, printer;
130 		for (int i = 0; m.FindString("m", i, &mimetype) == B_OK &&
131 			m.FindString("p", i, &printer ) == B_OK; i ++) {
132 			AddAppSettings(new AppSettings(mimetype.String(), printer.String()));
133 		}
134 			// restore printer settings
135 		BMessage page, job;
136 		for (int i = 0; m.FindString("P", i, &printer) == B_OK &&
137 			m.FindMessage("S", i, &page) == B_OK &&
138 			m.FindMessage("J", i, &job) == B_OK; i ++) {
139 			AddPrinterSettings(new PrinterSettings(printer.String(), &page, &job));
140 		}
141 
142 		if (m.FindBool("UseConfigWindow", &fUseConfigWindow) != B_OK)
143 			fUseConfigWindow = true;
144 
145 		if (m.FindRect("ConfigWindowFrame", &fConfigWindowFrame) != B_OK)
146 			fConfigWindowFrame = BRect(kConfigWindowFrame);
147 
148 		if (m.FindString("DefaultPrinter", &fDefaultPrinter) != B_OK)
149 			fDefaultPrinter = "";
150 	}
151 }
152