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