xref: /haiku/src/add-ons/tracker/zipomatic/ZipOMaticWindow.cpp (revision 1214ef1b2100f2b3299fc9d8d6142e46f70a4c3f)
1 /*
2  * Copyright 2003-2006, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Jonas Sundström, jonas.sundstrom@kirilla.com
7  */
8 
9 
10 #include <Debug.h>
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 
15 #include <Application.h>
16 #include <Roster.h>
17 #include <InterfaceKit.h>
18 #include <String.h>
19 #include <FindDirectory.h>
20 #include <Directory.h>
21 #include <Path.h>
22 #include <File.h>
23 
24 #include "ZipOMatic.h"
25 #include "ZipOMaticActivity.h"
26 #include "ZipOMaticMisc.h"
27 #include "ZipOMaticView.h"
28 #include "ZipOMaticWindow.h"
29 #include "ZipperThread.h"
30 
31 
32 ZippoWindow::ZippoWindow(BMessage * a_message)
33 : BWindow(BRect(200,200,430,310), "Zip-O-Matic", B_TITLED_WINDOW, B_NOT_V_RESIZABLE), // | B_NOT_ZOOMABLE),
34 	m_zippo_settings				(),
35 	m_zipper_thread					(NULL),
36 	m_got_refs_at_window_startup	(false),
37 	m_zipping_was_stopped			(false),
38 	m_alert_invoker_message			(new BMessage ('alrt')),
39 	m_alert_window_invoker			(new BInvoker (m_alert_invoker_message, NULL, this))
40 {
41 	PRINT(("ZippoWindow()\n"));
42 
43 //	Settings
44 	status_t	status	=	B_OK;
45 
46 	status	=	m_zippo_settings.SetTo("ZipOMatic.msg");
47 	if (status != B_OK)
48 		error_message("ZippoWindow() - m_zippo_settings.SetTo()", status);
49 
50 	status	=	m_zippo_settings.InitCheck();
51 	if (status != B_OK)
52 		error_message("ZippoWindow() - m_zippo_settings.InitCheck()", status);
53 
54 //	Interface
55 	zippoview  =  new ZippoView(Bounds());
56 
57 	AddChild (zippoview);
58 
59 	SetSizeLimits(Bounds().Width(), 15000, Bounds().Height(), Bounds().Height());
60 
61 //	Settings, (on-screen location of window)
62 	ReadSettings();
63 
64 //	Start zipper thread
65 	if (a_message != NULL)
66 	{
67 		m_got_refs_at_window_startup  =  true;
68 		StartZipping (a_message);
69 	}
70 }
71 
72 ZippoWindow::~ZippoWindow()
73 {
74 	PRINT(("ZippoWindow::~ZippoWindow()\n"));
75 
76 	//delete m_alert_invoker_message;
77 	delete m_alert_window_invoker;
78 
79 	// anything left to clean up?
80 }
81 
82 void
83 ZippoWindow::MessageReceived		(BMessage * a_message)
84 {
85 	switch(a_message->what)
86 	{
87 
88 		case B_REFS_RECEIVED:
89 						StartZipping (a_message);
90 						break;
91 
92 		case B_SIMPLE_DATA:
93 						if (IsZipping())
94 						{
95 							a_message->what = B_REFS_RECEIVED;
96 							be_app_messenger.SendMessage(a_message);
97 						}
98 						else
99 							StartZipping (a_message);
100 						break;
101 
102 		case 'exit':	// thread has finished		(finished, quit, killed, we don't know)
103 						// reset window state
104 						{
105 							m_zipper_thread = NULL;
106 							zippoview->m_activity_view->Stop();
107 							zippoview->m_archive_name_view->SetText(" ");
108 							if (m_zipping_was_stopped)
109 								zippoview->m_zip_output_view->SetText("Stopped");
110 							else
111 								zippoview->m_zip_output_view->SetText("Archive created OK");
112 
113 							CloseWindowOrKeepOpen();
114 							break;
115 						}
116 
117 		case 'exrr':	// thread has finished
118 						// reset window state
119 
120 						m_zipper_thread = NULL;
121 						zippoview->m_activity_view->Stop();
122 						zippoview->m_archive_name_view->SetText("");
123 						zippoview->m_zip_output_view->SetText("Error creating archive");
124 						//CloseWindowOrKeepOpen();
125 						break;
126 
127 		case 'strt':
128 					{
129 						BString archive_filename;
130 						if (a_message->FindString("archive_filename", & archive_filename) == B_OK)
131 							zippoview->m_archive_name_view->SetText(archive_filename.String());
132 						break;
133 					}
134 
135 		case 'outp':
136 					{
137 						BString zip_output;
138 						if (a_message->FindString("zip_output", & zip_output) == B_OK)
139 							zippoview->m_zip_output_view->SetText(zip_output.String());
140 						break;
141 					}
142 
143 		case 'alrt':
144 					{
145 						int32	which_button	=	-1;
146 						if (a_message->FindInt32("which", & which_button) == B_OK)
147 							if (which_button == 0)
148 								StopZipping();
149 							else
150 							{
151 								if (m_zipper_thread != NULL)
152 									m_zipper_thread->ResumeExternalZip();
153 
154 								zippoview->m_activity_view->Start();
155 							}
156 						break;
157 					}
158 
159 		default:	BWindow::MessageReceived(a_message);	break;
160 	}
161 }
162 
163 bool
164 ZippoWindow::QuitRequested	(void)
165 {
166 	PRINT(("ZippoWindow::QuitRequested()\n"));
167 
168 	if (m_zipper_thread == NULL)
169 	{
170 		WriteSettings();
171 		be_app_messenger.SendMessage(ZIPPO_WINDOW_QUIT);
172 		return true;
173 	}
174 	else
175 	{
176 		if (m_zipper_thread != NULL)
177 			m_zipper_thread->SuspendExternalZip();
178 
179 		zippoview->m_activity_view->Pause();
180 
181 		BAlert * quit_requester = new BAlert ("Stop or Continue", "Are you sure you want to "
182 									"stop creating this archive?", "Stop", "Continue",
183 									 NULL, B_WIDTH_AS_USUAL, B_INFO_ALERT);
184 		quit_requester->Go(m_alert_window_invoker);
185 		return false;
186 	}
187 }
188 
189 status_t
190 ZippoWindow::ReadSettings	(void)
191 {
192 	status_t	status	=	B_OK;
193 
194 	status	=	m_zippo_settings.InitCheck();
195 	if (status != B_OK)
196 		error_message("m_zippo_settings.InitCheck()", status);
197 
198 	status	=	m_zippo_settings.ReadSettings();
199 	if (status != B_OK)
200 		error_message("m_zippo_settings.ReadSettings()", status);
201 
202 	BRect	window_rect;
203 
204 	status	=	m_zippo_settings.FindRect("window_rect", & window_rect);
205 	if (status != B_OK)
206 	{
207 		error_message("m_settings_message->FindRect(window_rect)", status);
208 		return status;
209 	}
210 
211 	ResizeTo (window_rect.Width(), window_rect.Height());
212 	MoveTo (window_rect.LeftTop());
213 
214 	return B_OK;
215 }
216 
217 status_t
218 ZippoWindow::WriteSettings	(void)
219 {
220 	status_t	status	=	B_OK;
221 
222 	status	=	m_zippo_settings.InitCheck();
223 	if (status != B_OK)
224 		error_message("m_zippo_settings.InitCheck()", status);
225 
226 	status	=	m_zippo_settings.MakeEmpty();
227 	if (status != B_OK)
228 		error_message("m_zippo_settings.MakeEmpty()", status);
229 
230 	status	=	m_zippo_settings.AddRect("window_rect", Frame());
231 	if (status != B_OK)
232 	{
233 		error_message("m_settings_message->AddRect(window_rect)", status);
234 		return status;
235 	}
236 
237 	status	=	m_zippo_settings.WriteSettings();
238 	if (status != B_OK)
239 	{
240 		error_message("m_zippo_settings.WriteSettings()", status);
241 		return status;
242 	}
243 
244 
245 	return B_OK;
246 }
247 
248 void
249 ZippoWindow::StartZipping (BMessage * a_message)
250 {
251 	PRINT(("ZippoWindow::StartZipping()\n"));
252 
253 	zippoview->m_stop_button->SetEnabled(true);
254 	zippoview->m_activity_view->Start();
255 
256 	m_zipper_thread	=	new ZipperThread (a_message, this);
257 	m_zipper_thread->Start();
258 
259 	m_zipping_was_stopped  =  false;
260 }
261 
262 void
263 ZippoWindow::StopZipping (void)
264 {
265 	PRINT(("ZippoWindow::StopZipping()\n"));
266 
267 	m_zipping_was_stopped  =  true;
268 
269 	zippoview->m_stop_button->SetEnabled(false);
270 
271 	zippoview->m_activity_view->Stop();
272 
273 	m_zipper_thread->InterruptExternalZip();
274 	m_zipper_thread->Quit();
275 
276 	status_t  status  =  B_OK;
277 	m_zipper_thread->WaitForThread (& status);
278 	m_zipper_thread  =  NULL;
279 
280 	zippoview->m_archive_name_view->SetText(" ");
281 	zippoview->m_zip_output_view->SetText("Stopped");
282 
283 	CloseWindowOrKeepOpen();
284 }
285 
286 bool
287 ZippoWindow::IsZipping (void)
288 {
289 	if (m_zipper_thread == NULL)
290 		return false;
291 	else
292 		return true;
293 }
294 
295 void
296 ZippoWindow::CloseWindowOrKeepOpen (void)
297 {
298 	if (m_got_refs_at_window_startup)
299 		PostMessage(B_QUIT_REQUESTED);
300 }
301 
302 void
303 ZippoWindow::Zoom (BPoint origin, float width, float height)
304 {
305 	/*
306 	float archive_name_view_preferred_width;
307 	float zip_output_view_preferred_width;
308 	float throw_away_height;
309 	zippoview->GetPreferredSize(& archive_name_view_preferred_width, & throw_away_height);
310 	zippoview->GetPreferredSize(& zip_output_view_preferred_width, & throw_away_height);
311 	*/
312 
313 	//	BStringView::GetPreferredSize appears to be broken,
314 	//	so we have to use BView::StringWidth() instead
315 
316 	if (IsZipping())
317 	{
318 		float archive_name_view_preferred_width  =  zippoview->StringWidth(zippoview->m_archive_name_view->Text());
319 		float zip_output_view_preferred_width  =  zippoview->StringWidth(zippoview->m_zip_output_view->Text());
320 
321 		float the_wide_string;
322 
323 		if (zip_output_view_preferred_width > archive_name_view_preferred_width)
324 			the_wide_string  =  zip_output_view_preferred_width;
325 		else
326 			the_wide_string  =  archive_name_view_preferred_width;
327 
328 		ResizeTo(the_wide_string, Bounds().Height());
329 	}
330 	else
331 	{
332 		ResizeTo(230,110);
333 	}
334 }
335 
336