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 <stdio.h> 14 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> 23 24 #include "DataTranslationsWindow.h" 25 26 27 #undef B_TRANSLATION_CONTEXT 28 #define B_TRANSLATION_CONTEXT "DataTranslations" 29 30 31 const char* kApplicationSignature = "application/x-vnd.Haiku-DataTranslations"; 32 33 34 DataTranslationsApplication::DataTranslationsApplication() 35 : 36 BApplication(kApplicationSignature) 37 { 38 new DataTranslationsWindow(); 39 } 40 41 42 DataTranslationsApplication::~DataTranslationsApplication() 43 { 44 } 45 46 47 void 48 DataTranslationsApplication::_InstallError(const char* name, status_t status) 49 { 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(); 58 } 59 60 61 /*! 62 Installs the given entry in the target directory either by moving 63 or copying the entry. 64 */ 65 status_t 66 DataTranslationsApplication::_Install(BDirectory& target, BEntry& entry) 67 { 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; 72 73 // we need to copy the file 74 75 // TODO! 76 return B_ERROR; 77 } 78 79 80 void 81 DataTranslationsApplication::_NoTranslatorError(const char* name) 82 { 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(); 90 } 91 92 93 void 94 DataTranslationsApplication::RefsReceived(BMessage* message) 95 { 96 BTranslatorRoster* roster = BTranslatorRoster::Default(); 97 98 BPath path; 99 status_t status = find_directory(B_USER_ADDONS_DIRECTORY, &path, true); 100 if (status != B_OK) { 101 _InstallError("translator", status); 102 return; 103 } 104 105 BDirectory target; 106 status = target.SetTo(path.Path()); 107 if (status == B_OK) { 108 if (!target.Contains("Translators")) 109 status = target.CreateDirectory("Translators", &target); 110 else 111 status = target.SetTo(&target, "Translators"); 112 } 113 if (status != B_OK) { 114 _InstallError("translator", status); 115 return; 116 } 117 118 int32 i = 0; 119 entry_ref ref; 120 while (message->FindRef("refs", i++, &ref) == B_OK) { 121 if (!roster->IsTranslator(&ref)) { 122 _NoTranslatorError(ref.name); 123 continue; 124 } 125 126 BEntry entry(&ref, true); 127 status = entry.InitCheck(); 128 if (status != B_OK) { 129 _InstallError(ref.name, status); 130 continue; 131 } 132 133 if (target.Contains(ref.name)) { 134 BString string( 135 B_TRANSLATE("An item named '%name' already exists in the " 136 "Translators folder! Shall the existing translator be " 137 "overwritten?")); 138 string.ReplaceAll("%name", ref.name); 139 140 BAlert* alert = new BAlert(B_TRANSLATE("DataTranslations - Note"), 141 string.String(), B_TRANSLATE("Cancel"), 142 B_TRANSLATE("Overwrite")); 143 alert->SetShortcut(0, B_ESCAPE); 144 if (alert->Go() != 1) 145 continue; 146 147 // the original file will be replaced 148 } 149 150 // find out whether we need to copy it or not 151 152 status = _Install(target, entry); 153 if (status == B_OK) { 154 BAlert* alert = new BAlert(B_TRANSLATE("DataTranslations - Note"), 155 B_TRANSLATE("The new translator has been installed " 156 "successfully."), B_TRANSLATE("OK")); 157 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 158 alert->Go(NULL); 159 } else 160 _InstallError(ref.name, status); 161 } 162 } 163 164 165 // #pragma mark - 166 167 168 int 169 main(int, char**) 170 { 171 DataTranslationsApplication app; 172 app.Run(); 173 174 return 0; 175 } 176