xref: /haiku/src/preferences/datatranslations/DataTranslations.cpp (revision cbe35e2031cb2bfb757422f35006bb9bd382bed1)
1 /*
2  * DataTranslations.cpp
3  * DataTranslations mccall@digitalparadise.co.uk
4  *
5  */
6 
7 #include <Alert.h>
8 #include <Screen.h>
9 
10 #include "DataTranslations.h"
11 #include "DataTranslationsWindow.h"
12 #include "DataTranslationsSettings.h"
13 #include "DataTranslationsMessages.h"
14 
15 const char DataTranslationsApplication::kDataTranslationsApplicationSig[] = "application/x-vnd.OpenBeOS-prefs-translations";
16 
17 int main(int, char**)
18 {
19 	DataTranslationsApplication	myApplication;
20 
21 	myApplication.Run();
22 
23 	return(0);
24 }
25 
26 DataTranslationsApplication::DataTranslationsApplication()
27 					:BApplication(kDataTranslationsApplicationSig)
28 {
29 
30 	DataTranslationsWindow		*window;
31 
32 	fSettings = new DataTranslationsSettings();
33 
34 	window = new DataTranslationsWindow();
35 
36 }
37 
38 void
39 DataTranslationsApplication::SetWindowCorner(BPoint corner)
40 {
41 	fSettings->SetWindowCorner(corner);
42 }
43 
44 void
45 DataTranslationsApplication::AboutRequested(void)
46 {
47 	(new BAlert("", "... written by Oliver Siebenmarck", "Cool!"))->Go();
48 	return;
49 }
50 
51 DataTranslationsApplication::~DataTranslationsApplication()
52 {
53 	delete fSettings;
54 }
55 
56 void DataTranslationsApplication::Install_Done()
57 {
58 	(new BAlert("",
59 		"You have to quit and restart running applications\n"
60 		"for the installed Translators to be available in them.",
61 		"OK"))->Go();
62 }
63 
64 void DataTranslationsApplication::RefsReceived(BMessage *message)
65 {
66 	uint32 type;
67 	int32 count;
68 	entry_ref ref;
69 	BPath path;
70 	BString newfile;
71 
72 	char e_name[B_FILE_NAME_LENGTH];
73 
74 	message->GetInfo("refs", &type, &count);
75 	if (type != B_REF_TYPE)
76 		return;
77 
78     if (message->FindRef("refs", &ref) == B_OK) {
79     	BEntry entry(&ref, true);
80     	entry.GetName(e_name);
81 
82     	if (entry.IsFile() && entry.GetPath(&path) == B_OK) {
83     		BTranslatorRoster *roster = BTranslatorRoster::Default();
84 			if (roster->AddTranslators(path.Path()) == B_OK) {
85     			BDirectory dirt("/boot/home/config/add-ons/Translators/");
86     			newfile.SetTo("/boot/home/config/add-ons/Translators/");
87     			newfile << e_name; // Newfile with full path.
88 
89     			// Find out wether we need to copy it
90     			string.SetTo("");
91     			string << "The item '" << e_name << "' is now copied into the right place.\nWhat do you want to do with the original?";
92     			BAlert *keep = new BAlert("keep",  string.String() , "Remove", "Keep");
93     			keep->SetShortcut(1, B_ESCAPE);
94     			if (keep->Go() == 0)
95     				moveit = true;
96 
97     			if (moveit) // Just a quick move
98     			{
99     				if (dirt.Contains(newfile.String()))
100     				{
101     					string.SetTo("");
102     					string << "An item named '" << e_name <<"' already is in the \nTranslators folder";
103     					BAlert *myAlert = new BAlert("title",  string.String() , "Overwrite", "Stop");
104     					myAlert->SetShortcut(1, B_ESCAPE);
105 
106 						if (myAlert->Go() != 0)
107 							return;
108 						// File exists, we are still here. Kill it.
109 						BEntry dele;
110 						dirt.FindEntry(e_name, &dele);
111 						dele.Remove();
112 					}
113 					entry.MoveTo(&dirt); // Move it.
114     				Install_Done(); 	 // Installation done
115     				return;
116     			}
117 
118     			if (dirt.Contains(newfile.String()))
119     			{
120     				// File exists, What are we supposed to do now (Overwrite/_Stopp_?)
121     				string.SetTo("");
122     				string << "An item named '" << e_name <<"' already is in the \nTranslators folder";
123     				BAlert *myAlert = new BAlert("title",  string.String() , "Overwrite", "Stop");
124     				myAlert->SetShortcut(1, B_ESCAPE);
125 
126 					if (myAlert->Go() != 0)
127 						return;
128 				}
129 
130     			string.SetTo(path.Path()); // Fullpath+Filename
131 
132     			BString
133 				command	=	"copyattr -d -r ";
134 				command << string.String() << " " << newfile.String(); // Prepare Copy
135 
136 				system(command.String()); // Execute copy command
137 
138     			string.SetTo("");
139     			string << "Filename: " << e_name << "\nPath: " << path.Path() ;
140 
141     			// The new Translator has been installed by now.
142     			// And done we are
143     			Install_Done();
144     			return;
145     		}
146     		else
147     		{
148     			// Not a Translator
149     			no_trans(e_name);
150     		}
151     	}
152     	else
153     	{
154     		// Not even a file...
155     		no_trans(e_name);
156     	}
157     }
158 }
159 
160 void DataTranslationsApplication::no_trans(char item_name[B_FILE_NAME_LENGTH])
161 {
162 	BString text;
163 	text.SetTo("The item '");
164 	text << item_name << "' does not appear to be a Translator and will not be installed";
165 	(new BAlert("", text.String(), "Stop"))->Go();
166 	return;
167 }
168 
169