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