2 * Copyright 2002-2010, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT license.
7 * Andrew McCall, mccall@digitalparadise.co.uk
11 #include "DataTranslations.h"
17 #include <Directory.h>
19 #include <FindDirectory.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()
48 DataTranslationsApplication::_InstallError(const char* name
, status_t status
)
51 snprintf(text
.LockBuffer(512), 512,
52 B_TRANSLATE("Could not install %s:\n%s"), name
, strerror(status
));
54 BAlert
* alert
= new BAlert(B_TRANSLATE("DataTranslations - Error"),
55 text
.String(), B_TRANSLATE("OK"));
56 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
62 Installs the given entry in the target directory either by moving
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);
73 // we need to copy the file
81 DataTranslationsApplication::_NoTranslatorError(const char* name
)
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
);
94 DataTranslationsApplication::RefsReceived(BMessage
* message
)
96 BTranslatorRoster
* roster
= BTranslatorRoster::Default();
99 status_t status
= find_directory(B_USER_NONPACKAGED_ADDONS_DIRECTORY
,
101 if (status
!= B_OK
) {
102 _InstallError("translator", status
);
107 status
= target
.SetTo(path
.Path());
108 if (status
== B_OK
) {
109 if (!target
.Contains("Translators"))
110 status
= target
.CreateDirectory("Translators", &target
);
112 status
= target
.SetTo(&target
, "Translators");
114 if (status
!= B_OK
) {
115 _InstallError("translator", status
);
121 while (message
->FindRef("refs", i
++, &ref
) == B_OK
) {
122 if (!roster
->IsTranslator(&ref
)) {
123 _NoTranslatorError(ref
.name
);
127 BEntry
entry(&ref
, true);
128 status
= entry
.InitCheck();
129 if (status
!= B_OK
) {
130 _InstallError(ref
.name
, status
);
134 if (target
.Contains(ref
.name
)) {
136 B_TRANSLATE("An item named '%name' already exists in the "
137 "Translators folder! Shall the existing translator be "
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)
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
);
161 _InstallError(ref
.name
, status
);
172 DataTranslationsApplication app
;