1 // license: public domain 2 // authors: jonas.sundstrom@kirilla.com 3 4 #include <TrackerAddOn.h> 5 #include <Roster.h> 6 #include <Debug.h> 7 8 #include "ZipOMatic.h" 9 #include "ZipOMaticMisc.h" 10 #include "ZipOMaticWindow.h" 11 12 extern "C" void 13 process_refs(entry_ref dir_ref, BMessage * msg, void *) 14 { 15 msg->AddRef("dir_ref", & dir_ref); 16 17 status_t status = B_OK; 18 type_code ref_type = B_REF_TYPE; 19 int32 ref_count = 0; 20 21 status = msg->GetInfo("refs", & ref_type, & ref_count); 22 if (status != B_OK || ref_count < 1) 23 be_roster->Launch (ZIPOMATIC_APP_SIG); 24 else 25 be_roster->Launch (ZIPOMATIC_APP_SIG, msg ); 26 } 27 28 int main() 29 { 30 ZipOMatic app; 31 app.Run(); 32 33 return (0); 34 } 35 36 ZipOMatic::ZipOMatic (void) 37 : BApplication (ZIPOMATIC_APP_SIG), 38 m_got_refs (false) 39 { 40 PRINT(("ZipOMatic::ZipOMatic()\n")); 41 42 // void 43 } 44 45 ZipOMatic::~ZipOMatic (void) 46 { 47 PRINT(("ZipOMatic::~ZipOMatic()\n")); 48 49 fflush(stdout); 50 } 51 52 void 53 ZipOMatic::RefsReceived (BMessage * a_message) 54 { 55 PRINT(("ZipOMatic::RefsReceived()\n")); 56 57 if (IsLaunching()) 58 m_got_refs = true; 59 60 BMessage * msg = new BMessage (* a_message); 61 62 UseExistingOrCreateNewWindow(msg); 63 } 64 65 void 66 ZipOMatic::ReadyToRun (void) 67 { 68 PRINT(("ZipOMatic::ReadyToRun()\n")); 69 70 if (m_got_refs) 71 { 72 // nothing - wait on window(s) to finish 73 } 74 else 75 UseExistingOrCreateNewWindow(); 76 } 77 78 void 79 ZipOMatic::MessageReceived (BMessage * a_message) 80 { 81 PRINT(("ZipOMatic::MessageReceived()\n")); 82 83 switch(a_message->what) 84 { 85 case ZIPPO_WINDOW_QUIT: 86 87 snooze (200000); 88 if (CountWindows() == 0) 89 Quit(); 90 break; 91 92 case B_SILENT_RELAUNCH: 93 94 SilentRelaunch(); 95 break; 96 97 default: BApplication::MessageReceived(a_message); break; 98 } 99 100 } 101 102 bool 103 ZipOMatic::QuitRequested (void) 104 { 105 // intelligent (?) closing of the windows 106 // 107 // overriding BApplication::QuitRequested(); 108 109 if (CountWindows() <= 0) 110 return true; 111 112 BList window_list (5); 113 int32 window_count = 0; 114 BWindow * window; 115 116 // build list of windows 117 while (1) 118 { 119 window = WindowAt(window_count++); 120 if (window == NULL) 121 break; 122 123 window_list.AddItem(window); 124 } 125 126 // ask windows to quit 127 while (1) 128 { 129 window = (BWindow *) window_list.RemoveItem(int32(0)); 130 if (window == NULL) 131 break; 132 133 if (window->Lock()) 134 { 135 window->PostMessage(B_QUIT_REQUESTED); 136 window->Unlock(); 137 } 138 } 139 140 PRINT(("CountWindows(): %ld\n", CountWindows())); 141 142 if (CountWindows() <= 0) 143 return true; 144 145 return false; // default: stay alive 146 } 147 148 void 149 ZipOMatic::SilentRelaunch (void) 150 { 151 UseExistingOrCreateNewWindow(); 152 } 153 154 void 155 ZipOMatic::UseExistingOrCreateNewWindow (BMessage * a_message) 156 { 157 int32 window_count = 0; 158 ZippoWindow * window; 159 bool found_non_busy_window = false; 160 161 while (1) 162 { 163 window = dynamic_cast<ZippoWindow *>(WindowAt(window_count++)); 164 if (window == NULL) 165 break; 166 167 if (window->Lock()) 168 { 169 if (! window->IsZipping()) 170 { 171 found_non_busy_window = true; 172 if (a_message != NULL) 173 window->PostMessage(a_message); 174 window->Activate(); 175 window->Unlock(); 176 break; 177 } 178 window->Unlock(); 179 } 180 } 181 182 if (! found_non_busy_window) 183 { 184 ZippoWindow * m_window = new ZippoWindow(a_message); 185 m_window->Show(); 186 } 187 } 188