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