usb_ecm: Use the current configuration instead of a fixed one.
[haiku.git] / src / preferences / datatranslations / DataTranslations.cpp
blob045d0d742a0e20077624761e109944d29b0dc122
1 /*
2 * Copyright 2002-2010, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT license.
5 * Authors:
6 * Oliver Siebenmarck
7 * Andrew McCall, mccall@digitalparadise.co.uk
8 * Michael Wilber
9 */
11 #include "DataTranslations.h"
13 #include <stdio.h>
15 #include <Alert.h>
16 #include <Catalog.h>
17 #include <Directory.h>
18 #include <Entry.h>
19 #include <FindDirectory.h>
20 #include <String.h>
21 #include <TextView.h>
22 #include <TranslatorRoster.h>
24 #include "DataTranslationsWindow.h"
27 #undef B_TRANSLATION_CONTEXT
28 #define B_TRANSLATION_CONTEXT "DataTranslations"
31 const char* kApplicationSignature = "application/x-vnd.Haiku-DataTranslations";
34 DataTranslationsApplication::DataTranslationsApplication()
36 BApplication(kApplicationSignature)
38 new DataTranslationsWindow();
42 DataTranslationsApplication::~DataTranslationsApplication()
47 void
48 DataTranslationsApplication::_InstallError(const char* name, status_t status)
50 BString text;
51 snprintf(text.LockBuffer(512), 512,
52 B_TRANSLATE("Could not install %s:\n%s"), name, strerror(status));
53 text.UnlockBuffer();
54 BAlert* alert = new BAlert(B_TRANSLATE("DataTranslations - Error"),
55 text.String(), B_TRANSLATE("OK"));
56 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
57 alert->Go();
61 /*!
62 Installs the given entry in the target directory either by moving
63 or copying the entry.
65 status_t
66 DataTranslationsApplication::_Install(BDirectory& target, BEntry& entry)
68 // Find out whether we need to copy it
69 status_t status = entry.MoveTo(&target, NULL, true);
70 if (status == B_OK)
71 return B_OK;
73 // we need to copy the file
75 // TODO!
76 return B_ERROR;
80 void
81 DataTranslationsApplication::_NoTranslatorError(const char* name)
83 BString text(
84 B_TRANSLATE("The item '%name' does not appear to be a Translator and "
85 "will not be installed."));
86 text.ReplaceAll("%name", name);
87 BAlert* alert = new BAlert("", text.String(), B_TRANSLATE("OK"));
88 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
89 alert->Go();
93 void
94 DataTranslationsApplication::RefsReceived(BMessage* message)
96 BTranslatorRoster* roster = BTranslatorRoster::Default();
98 BPath path;
99 status_t status = find_directory(B_USER_NONPACKAGED_ADDONS_DIRECTORY,
100 &path, true);
101 if (status != B_OK) {
102 _InstallError("translator", status);
103 return;
106 BDirectory target;
107 status = target.SetTo(path.Path());
108 if (status == B_OK) {
109 if (!target.Contains("Translators"))
110 status = target.CreateDirectory("Translators", &target);
111 else
112 status = target.SetTo(&target, "Translators");
114 if (status != B_OK) {
115 _InstallError("translator", status);
116 return;
119 int32 i = 0;
120 entry_ref ref;
121 while (message->FindRef("refs", i++, &ref) == B_OK) {
122 if (!roster->IsTranslator(&ref)) {
123 _NoTranslatorError(ref.name);
124 continue;
127 BEntry entry(&ref, true);
128 status = entry.InitCheck();
129 if (status != B_OK) {
130 _InstallError(ref.name, status);
131 continue;
134 if (target.Contains(ref.name)) {
135 BString string(
136 B_TRANSLATE("An item named '%name' already exists in the "
137 "Translators folder! Shall the existing translator be "
138 "overwritten?"));
139 string.ReplaceAll("%name", ref.name);
141 BAlert* alert = new BAlert(B_TRANSLATE("DataTranslations - Note"),
142 string.String(), B_TRANSLATE("Cancel"),
143 B_TRANSLATE("Overwrite"));
144 alert->SetShortcut(0, B_ESCAPE);
145 if (alert->Go() != 1)
146 continue;
148 // the original file will be replaced
151 // find out whether we need to copy it or not
153 status = _Install(target, entry);
154 if (status == B_OK) {
155 BAlert* alert = new BAlert(B_TRANSLATE("DataTranslations - Note"),
156 B_TRANSLATE("The new translator has been installed "
157 "successfully."), B_TRANSLATE("OK"));
158 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
159 alert->Go(NULL);
160 } else
161 _InstallError(ref.name, status);
166 // #pragma mark -
170 main(int, char**)
172 DataTranslationsApplication app;
173 app.Run();
175 return 0;