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_TRANSLATE_CONTEXT 28 #define B_TRANSLATE_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->Go(); 57 } 58 59 60 /*! 61 Installs the given entry in the target directory either by moving 62 or copying the entry. 63 */ 64 status_t 65 DataTranslationsApplication::_Install(BDirectory& target, BEntry& entry) 66 { 67 // Find out wether we need to copy it 68 status_t status = entry.MoveTo(&target, NULL, true); 69 if (status == B_OK) 70 return B_OK; 71 72 // we need to copy the file 73 74 // TODO! 75 return B_ERROR; 76 } 77 78 79 void 80 DataTranslationsApplication::_NoTranslatorError(const char* name) 81 { 82 BString text( 83 B_TRANSLATE("The item '%name' does not appear to be a Translator and " 84 "will not be installed.")); 85 text.ReplaceAll("%name", name); 86 BAlert* alert = new BAlert("", text.String(), B_TRANSLATE("Ok")); 87 alert->Go(); 88 } 89 90 91 void 92 DataTranslationsApplication::RefsReceived(BMessage* message) 93 { 94 BTranslatorRoster* roster = BTranslatorRoster::Default(); 95 96 BPath path; 97 status_t status = find_directory(B_USER_ADDONS_DIRECTORY, &path, true); 98 if (status != B_OK) { 99 _InstallError("translator", status); 100 return; 101 } 102 103 BDirectory target; 104 status = target.SetTo(path.Path()); 105 if (status == B_OK) { 106 if (!target.Contains("Translators")) 107 status = target.CreateDirectory("Translators", &target); 108 else 109 status = target.SetTo(&target, "Translators"); 110 } 111 if (status != B_OK) { 112 _InstallError("translator", status); 113 return; 114 } 115 116 int32 i = 0; 117 entry_ref ref; 118 while (message->FindRef("refs", i++, &ref) == B_OK) { 119 if (!roster->IsTranslator(&ref)) { 120 _NoTranslatorError(ref.name); 121 continue; 122 } 123 124 BEntry entry(&ref, true); 125 status = entry.InitCheck(); 126 if (status != B_OK) { 127 _InstallError(ref.name, status); 128 continue; 129 } 130 131 if (target.Contains(ref.name)) { 132 BString string( 133 B_TRANSLATE("An item named '%name' already exists in the " 134 "Translators folder! Shall the existing translator be " 135 "overwritten?")); 136 string.ReplaceAll("%name", ref.name); 137 138 BAlert* alert = new BAlert(B_TRANSLATE("DataTranslations - Note"), 139 string.String(), B_TRANSLATE("Cancel"), 140 B_TRANSLATE("Overwrite")); 141 alert->SetShortcut(0, B_ESCAPE); 142 if (alert->Go() != 1) 143 continue; 144 145 // the original file will be replaced 146 } 147 148 // find out wether we need to copy it or not 149 150 status = _Install(target, entry); 151 if (status == B_OK) { 152 BAlert* alert = new BAlert(B_TRANSLATE("DataTranslations - Note"), 153 B_TRANSLATE("The new translator has been installed " 154 "successfully."), B_TRANSLATE("OK")); 155 alert->Go(NULL); 156 } else 157 _InstallError(ref.name, status); 158 } 159 } 160 161 162 // #pragma mark - 163 164 165 int 166 main(int, char**) 167 { 168 DataTranslationsApplication app; 169 app.Run(); 170 171 return 0; 172 } 173