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_NONPACKAGED_ADDONS_DIRECTORY, 100 &path, true); 101 if (status != B_OK) { 102 _InstallError("translator", status); 103 return; 104 } 105 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"); 113 } 114 if (status != B_OK) { 115 _InstallError("translator", status); 116 return; 117 } 118 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; 125 } 126 127 BEntry entry(&ref, true); 128 status = entry.InitCheck(); 129 if (status != B_OK) { 130 _InstallError(ref.name, status); 131 continue; 132 } 133 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); 140 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; 147 148 // the original file will be replaced 149 } 150 151 // find out whether we need to copy it or not 152 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); 162 } 163 } 164 165 166 // #pragma mark - 167 168 169 int 170 main(int, char**) 171 { 172 DataTranslationsApplication app; 173 app.Run(); 174 175 return 0; 176 } 177