xref: /haiku/src/servers/print/Settings.cpp (revision 0c93c0a807b27096abbfad677436afb7d1712d4a)
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 // Implementation of AppSettings
13 
14 AppSettings::AppSettings(const char* mimetype, const char* printer)
15 	: fMimeType(mimetype)
16 {
17 	if (printer) fPrinter = printer;
18 }
19 
20 
21 // Implementation of PrinterSettings
22 
23 PrinterSettings::PrinterSettings(const char* printer, BMessage* pageSettings, BMessage* jobSettings)
24 	: fPrinter(printer)
25 {
26 	if (pageSettings) fPageSettings = *pageSettings;
27 	if (jobSettings) fJobSettings = *jobSettings;
28 }
29 
30 
31 // Implementation of Settings
32 
33 Settings* Settings::fSingleton = NULL;
34 
35 static const BRect kConfigWindowFrame(30, 30, 220, 120);
36 
37 Settings::Settings()
38 	: fApps(true)     // owns AppSettings
39 	, fPrinters(true) // owns PrinterSettings
40 	, fUseConfigWindow(true)
41 	, fConfigWindowFrame(kConfigWindowFrame)
42 {
43 }
44 
45 Settings::~Settings() {
46 	fSingleton = NULL;
47 }
48 
49 Settings* Settings::GetSettings() {
50 	if (fSingleton == NULL) fSingleton = new Settings();
51 	return fSingleton;
52 }
53 
54 void Settings::RemoveAppSettings(int i) {
55 	delete fApps.RemoveItemAt(i);
56 }
57 
58 void Settings::RemovePrinterSettings(int i) {
59 	delete fPrinters.RemoveItemAt(i);
60 }
61 
62 AppSettings* Settings::FindAppSettings(const char* mimeType) {
63 	for (int i = AppSettingsCount()-1; i >= 0; i --) {
64 		if (strcmp(AppSettingsAt(i)->GetMimeType(), mimeType) == 0)
65 			return AppSettingsAt(i);
66 	}
67 	return NULL;
68 }
69 
70 PrinterSettings* Settings::FindPrinterSettings(const char* printer) {
71 	for (int i = PrinterSettingsCount()-1; i >= 0; i --) {
72 		if (strcmp(PrinterSettingsAt(i)->GetPrinter(), printer) == 0)
73 			return PrinterSettingsAt(i);
74 	}
75 	return NULL;
76 }
77 
78 void Settings::Save(BFile* file) {
79 	BMessage m;
80 		// store application settings
81 	for (int i = 0; i < AppSettingsCount(); i++) {
82 		AppSettings* app = AppSettingsAt(i);
83 		m.AddString("m", app->GetMimeType());
84 		m.AddString("p", app->GetPrinter());
85 	}
86 		// store printer settings
87 	for (int i = 0; i < PrinterSettingsCount(); i++) {
88 		PrinterSettings* p = PrinterSettingsAt(i);
89 		m.AddString("P", p->GetPrinter());
90 		m.AddMessage("S", p->GetPageSettings());
91 		m.AddMessage("J", p->GetJobSettings());
92 	}
93 
94 	m.AddBool("UseConfigWindow", fUseConfigWindow);
95 	m.AddRect("ConfigWindowFrame", fConfigWindowFrame);
96 	m.AddString("DefaultPrinter", fDefaultPrinter);
97 	m.Flatten(file);
98 }
99 
100 void Settings::Load(BFile* file) {
101 	BMessage m;
102 	if (m.Unflatten(file) == B_OK) {
103 			// restore application settings
104 		BString mimetype, printer;
105 		for (int i = 0; m.FindString("m", i, &mimetype) == B_OK &&
106 			m.FindString("p", i, &printer ) == B_OK; i ++) {
107 			AddAppSettings(new AppSettings(mimetype.String(), printer.String()));
108 		}
109 			// restore printer settings
110 		BMessage page, job;
111 		for (int i = 0; m.FindString("P", i, &printer) == B_OK &&
112 			m.FindMessage("S", i, &page) == B_OK &&
113 			m.FindMessage("J", i, &job) == B_OK; i ++) {
114 			AddPrinterSettings(new PrinterSettings(printer.String(), &page, &job));
115 		}
116 
117 		if (m.FindBool("UseConfigWindow", &fUseConfigWindow) != B_OK)
118 			fUseConfigWindow = true;
119 
120 		if (m.FindRect("ConfigWindowFrame", &fConfigWindowFrame) != B_OK)
121 			fConfigWindowFrame = BRect(kConfigWindowFrame);
122 
123 		if (m.FindString("DefaultPrinter", &fDefaultPrinter) != B_OK)
124 			fDefaultPrinter = "";
125 	}
126 }
127