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