xref: /haiku/src/preferences/datatranslations/DataTranslations.cpp (revision 76e9533e9e9945f62053bf7c737df2f3e1735bbd)
1 /*
2  * Copyright 2002-2010, Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Oliver Siebenmarck
7  *		Andrew McCall, mccall@digitalparadise.co.uk
8  *		Michael Wilber
9  */
10 
11 #include "DataTranslations.h"
12 
13 #include <Alert.h>
14 #include <Catalog.h>
15 #include <Directory.h>
16 #include <FindDirectory.h>
17 #include <String.h>
18 #include <TranslatorRoster.h>
19 #include <TextView.h>
20 
21 #include "DataTranslationsSettings.h"
22 #include "DataTranslationsWindow.h"
23 
24 
25 #undef B_TRANSLATE_CONTEXT
26 #define B_TRANSLATE_CONTEXT "DataTranslations"
27 
28 
29 const char* kApplicationSignature = "application/x-vnd.Haiku-DataTranslations";
30 
31 
32 DataTranslationsApplication::DataTranslationsApplication()
33 	: BApplication(kApplicationSignature)
34 {
35 	new DataTranslationsWindow();
36 }
37 
38 
39 DataTranslationsApplication::~DataTranslationsApplication()
40 {
41 }
42 
43 
44 void
45 DataTranslationsApplication::SetWindowCorner(const BPoint& leftTop)
46 {
47 	fSettings.SetWindowCorner(leftTop);
48 }
49 
50 
51 void
52 DataTranslationsApplication::AboutRequested()
53 {
54 	BAlert* alert = new BAlert(B_TRANSLATE("about"),
55 		B_TRANSLATE("DataTranslations\n\twritten by Oliver Siebenmarck and"
56 		" others\n\tCopyright 2002-2010, Haiku Inc. All rights reserved.\n"),
57 		B_TRANSLATE("OK"));
58 
59 	BTextView* view = alert->TextView();
60 	view->SetStylable(true);
61 
62 	BFont font;
63 	view->GetFont(&font);
64 	font.SetSize(18);
65 	font.SetFace(B_BOLD_FACE);
66 	view->SetFontAndColor(0, 16, &font);
67 
68 	alert->Go();
69 }
70 
71 
72 void
73 DataTranslationsApplication::_InstallError(const char* name, status_t status)
74 {
75 	BString text(B_TRANSLATE("Could not install "));
76 	text << name << ":\n" << strerror(status);
77 	BAlert* alert = new BAlert(B_TRANSLATE("DataTranslations - Error"),
78 		text.String(), B_TRANSLATE("Ok"));
79 	alert->Go();
80 }
81 
82 
83 /*!
84 	Installs the given entry in the target directory either by moving
85 	or copying the entry.
86 */
87 status_t
88 DataTranslationsApplication::_Install(BDirectory& target, BEntry& entry)
89 {
90 	// Find out wether we need to copy it
91 	status_t status = entry.MoveTo(&target, NULL, true);
92 	if (status == B_OK)
93 		return B_OK;
94 
95 	// we need to copy the file
96 
97 	// TODO!
98 	return B_ERROR;
99 }
100 
101 
102 void
103 DataTranslationsApplication::_NoTranslatorError(const char* name)
104 {
105 	BString text(B_TRANSLATE("The item '"));
106 	text << B_TRANSLATE("%name' does not appear to be a Translator and will"
107 		" not be installed.");
108 	text.ReplaceAll("%name", name);
109 	BAlert* alert = new BAlert("", text.String(), B_TRANSLATE("Ok"));
110 	alert->Go();
111 }
112 
113 
114 void
115 DataTranslationsApplication::RefsReceived(BMessage* message)
116 {
117 	BTranslatorRoster* roster = BTranslatorRoster::Default();
118 
119 	BPath path;
120 	status_t status = find_directory(B_USER_ADDONS_DIRECTORY, &path, true);
121 	if (status != B_OK) {
122 		_InstallError("translator", status);
123 		return;
124 	}
125 
126 	BDirectory target;
127 	status = target.SetTo(path.Path());
128 	if (status == B_OK) {
129 		if (!target.Contains("Translators"))
130 			status = target.CreateDirectory("Translators", &target);
131 		else
132 			status = target.SetTo(&target, "Translators");
133 	}
134 	if (status != B_OK) {
135 		_InstallError("translator", status);
136 		return;
137 	}
138 
139 	int32 i = 0;
140 	entry_ref ref;
141 	while (message->FindRef("refs", i++, &ref) == B_OK) {
142 		if (!roster->IsTranslator(&ref)) {
143 			_NoTranslatorError(ref.name);
144 			continue;
145 		}
146 
147 		BEntry entry(&ref, true);
148 		status = entry.InitCheck();
149 		if (status != B_OK) {
150 			_InstallError(ref.name, status);
151 			continue;
152 		}
153 
154 		if (target.Contains(ref.name)) {
155 			BString string(B_TRANSLATE("An item named '"));
156 			string << B_TRANSLATE("%name already exists in the "
157 				"Translators folder! Shall the existing translator be "
158 				"overwritten?");
159 			string.ReplaceAll("%name", ref.name);
160 
161 			BAlert* alert = new BAlert(B_TRANSLATE("DataTranslations - Note"),
162 				string.String(), B_TRANSLATE("Cancel"),
163 				B_TRANSLATE("Overwrite"));
164 			alert->SetShortcut(0, B_ESCAPE);
165 			if (alert->Go() != 1)
166 				continue;
167 
168 			// the original file will be replaced
169 		}
170 
171 		// find out wether we need to copy it or not
172 
173 		status = _Install(target, entry);
174 		if (status == B_OK) {
175 			BAlert* alert = new BAlert(B_TRANSLATE("DataTranslations - Note"),
176 				B_TRANSLATE("The new translator has been installed "
177 					"successfully."), B_TRANSLATE("OK"));
178 			alert->Go(NULL);
179 		} else
180 			_InstallError(ref.name, status);
181 	}
182 }
183 
184 
185 //	#pragma mark -
186 
187 
188 int
189 main(int, char**)
190 {
191 	DataTranslationsApplication app;
192 	app.Run();
193 
194 	return 0;
195 }
196