xref: /haiku/src/kits/mail/FileConfigView.cpp (revision 4bd0c1066b227cec4b79883bdef697c7a27f2e90)
1 /*
2  * Copyright 2004-2012, Haiku, Inc. All rights reserved.
3  * Copyright 2001 Dr. Zoidberg Enterprises. All rights reserved.
4  *
5  * Distributed under the terms of the MIT License.
6  */
7 
8 
9 //! A file configuration view for filters
10 
11 
12 #include <FileConfigView.h>
13 
14 #include <stdio.h>
15 
16 #include <Button.h>
17 #include <Catalog.h>
18 #include <GroupLayout.h>
19 #include <MailSettingsView.h>
20 #include <Message.h>
21 #include <Path.h>
22 #include <String.h>
23 #include <TextControl.h>
24 
25 
26 #undef B_TRANSLATION_CONTEXT
27 #define B_TRANSLATION_CONTEXT "MailKit"
28 
29 
30 static const uint32 kMsgSelectButton = 'fsel';
31 
32 
33 namespace BPrivate {
34 
35 
36 FileControl::FileControl(const char* name, const char* label,
37 	const char* pathOfFile, uint32 flavors)
38 	:
39 	BView(name, 0)
40 {
41 	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
42 	SetLayout(new BGroupLayout(B_HORIZONTAL));
43 
44 	fText = new BTextControl("file_path", label, pathOfFile, NULL);
45 	AddChild(fText);
46 
47 	fButton = new BButton("select_file", B_TRANSLATE("Select" B_UTF8_ELLIPSIS),
48 		new BMessage(kMsgSelectButton));
49 	AddChild(fButton);
50 
51 	fPanel = new BFilePanel(B_OPEN_PANEL, NULL, NULL, flavors, false);
52 }
53 
54 
55 FileControl::~FileControl()
56 {
57 	delete fPanel;
58 }
59 
60 
61 void
62 FileControl::AttachedToWindow()
63 {
64 	fButton->SetTarget(this);
65 	fPanel->SetTarget(this);
66 }
67 
68 
69 void
70 FileControl::MessageReceived(BMessage* msg)
71 {
72 	switch (msg->what) {
73 		case kMsgSelectButton:
74 		{
75 			fPanel->Hide();
76 
77 			BPath path(fText->Text());
78 			if (path.InitCheck() == B_OK && path.GetParent(&path) == B_OK)
79 				fPanel->SetPanelDirectory(path.Path());
80 
81 			fPanel->Show();
82 			break;
83 		}
84 		case B_REFS_RECEIVED:
85 		{
86 			entry_ref ref;
87 			if (msg->FindRef("refs", &ref) == B_OK) {
88 				BEntry entry(&ref);
89 				if (entry.InitCheck() == B_OK) {
90 					BPath path;
91 					entry.GetPath(&path);
92 
93 					fText->SetText(path.Path());
94 				}
95 			}
96 			break;
97 		}
98 
99 		default:
100 			BView::MessageReceived(msg);
101 			break;
102 	}
103 }
104 
105 
106 void
107 FileControl::SetText(const char* pathOfFile)
108 {
109 	fText->SetText(pathOfFile);
110 }
111 
112 
113 const char*
114 FileControl::Text() const
115 {
116 	return fText->Text();
117 }
118 
119 
120 void
121 FileControl::SetEnabled(bool enabled)
122 {
123 	fText->SetEnabled(enabled);
124 	fButton->SetEnabled(enabled);
125 }
126 
127 
128 //	#pragma mark -
129 
130 
131 MailFileConfigView::MailFileConfigView(const char* label, const char* name,
132 	bool useMeta, const char* defaultPath, uint32 flavors)
133 	:
134 	FileControl(name, label, defaultPath, flavors),
135 	fUseMeta(useMeta),
136 	fName(name)
137 {
138 }
139 
140 
141 void
142 MailFileConfigView::SetTo(const BMessage* archive, BMessage* meta)
143 {
144 	SetText((fUseMeta ? meta : archive)->FindString(fName));
145 	fMeta = meta;
146 }
147 
148 
149 status_t
150 MailFileConfigView::SaveInto(BMailAddOnSettings& settings) const
151 {
152 	BMessage* archive = fUseMeta ? fMeta : &settings;
153 	return archive->SetString(fName, Text());
154 }
155 
156 
157 }	// namespace BPrivate
158