2 * Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
15 #include <package/PackageInfo.h>
23 #include "FeaturedPackagesView.h"
25 #include "MainWindow.h"
26 #include "ServerSettings.h"
27 #include "ScreenshotWindow.h"
30 #undef B_TRANSLATION_CONTEXT
31 #define B_TRANSLATION_CONTEXT "App"
36 BApplication("application/x-vnd.Haiku-HaikuDepot"),
41 _CheckPackageDaemonRuns();
47 // We cannot let global destructors cleanup static BitmapRef objects,
48 // since calling BBitmap destructors needs a valid BApplication still
49 // around. That's why we do it here.
50 PackageInfo::CleanupDefaultIcon();
51 FeaturedPackagesView::CleanupIcons();
52 ScreenshotWindow::CleanupIcons();
59 if (fMainWindow
!= NULL
60 && fMainWindow
->LockLooperWithTimeout(1500000) == B_OK
) {
61 BMessage windowSettings
;
62 fMainWindow
->StoreSettings(windowSettings
);
64 fMainWindow
->UnlockLooper();
66 _StoreSettings(windowSettings
);
80 _LoadSettings(settings
);
82 fMainWindow
= new MainWindow(settings
);
83 _ShowWindow(fMainWindow
);
88 App::MessageReceived(BMessage
* message
)
90 switch (message
->what
) {
91 case MSG_MAIN_WINDOW_CLOSED
:
93 BMessage windowSettings
;
94 if (message
->FindMessage("window settings",
95 &windowSettings
) == B_OK
) {
96 _StoreSettings(windowSettings
);
100 if (fWindowCount
== 0)
106 BApplication::MessageReceived(message
);
113 App::RefsReceived(BMessage
* message
)
117 while (message
->FindRef("refs", index
++, &ref
) == B_OK
) {
118 BEntry
entry(&ref
, true);
128 WEB_APP_BASE_URL_SWITCH
,
136 fprintf(stdout
, "HaikuDepot ");
137 fprintf(stdout
, "[-u|--webappbaseurl <web-app-base-url>] ");
138 fprintf(stdout
, "[-v|--verbosity [off|info|debug|trace] ");
139 fprintf(stdout
, "[-h|--help]\n\n");
140 fprintf(stdout
, "'-h' : causes this help text to be printed out.\n");
141 fprintf(stdout
, "'-v' : allows for the verbosity level to be set.\n");
142 fprintf(stdout
, "'-u' : allows for the haiku depot server to be\n");
143 fprintf(stdout
, " configured.");
148 app_resolve_switch(char *arg
)
150 int arglen
= strlen(arg
);
152 if (arglen
> 0 && arg
[0] == '-') {
154 if (arglen
> 3 && arg
[1] == '-') { // long form
155 if (0 == strcmp(&arg
[2], "webappbaseurl"))
156 return WEB_APP_BASE_URL_SWITCH
;
158 if (0 == strcmp(&arg
[2], "help"))
161 if (0 == strcmp(&arg
[2], "verbosity"))
162 return VERBOSITY_SWITCH
;
164 if (arglen
== 2) { // short form
167 return WEB_APP_BASE_URL_SWITCH
;
173 return VERBOSITY_SWITCH
;
178 return UNKNOWN_SWITCH
;
186 App::ArgvReceived(int32 argc
, char* argv
[])
188 for (int i
= 1; i
< argc
;) {
190 // check to make sure that if there is a value for the switch,
191 // that the value is in fact supplied.
193 switch (app_resolve_switch(argv
[i
])) {
194 case VERBOSITY_SWITCH
:
195 case WEB_APP_BASE_URL_SWITCH
:
197 fprintf(stdout
, "unexpected end of arguments; missing "
198 "value for switch [%s]\n", argv
[i
]);
208 // now process each switch.
210 switch (app_resolve_switch(argv
[i
])) {
212 case VERBOSITY_SWITCH
:
213 if (!Logger::SetLevelByName(argv
[i
+1])) {
214 fprintf(stdout
, "unknown log level [%s]\n", argv
[i
+ 1]);
217 i
++; // also move past the log level value
225 case WEB_APP_BASE_URL_SWITCH
:
226 if (ServerSettings::SetBaseUrl(BUrl(argv
[i
+ 1])) != B_OK
) {
227 fprintf(stdout
, "malformed web app base url; %s\n",
232 fprintf(stdout
, "did configure the web base url; %s\n",
236 i
++; // also move past the url value
242 BEntry
entry(argv
[i
], true);
248 fprintf(stdout
, "unknown switch; %s\n", argv
[i
]);
253 i
++; // move on at least one arg
258 // #pragma mark - private
262 App::_Open(const BEntry
& entry
)
265 if (!entry
.Exists() || entry
.GetPath(&path
) != B_OK
) {
266 fprintf(stderr
, "Package file not found: %s\n", path
.Path());
270 // Try to parse package file via Package Kit
271 BPackageKit::BPackageInfo info
;
272 status_t status
= info
.ReadFromPackageFile(path
.Path());
273 if (status
!= B_OK
) {
274 fprintf(stderr
, "Failed to parse package file: %s\n",
279 // Transfer information into PackageInfo
280 PackageInfoRef
package(new(std::nothrow
) PackageInfo(info
), true);
281 if (package
.Get() == NULL
) {
282 fprintf(stderr
, "Could not allocate PackageInfo\n");
286 package
->SetLocalFilePath(path
.Path());
289 _LoadSettings(settings
);
291 MainWindow
* window
= new MainWindow(settings
, package
);
297 App::_ShowWindow(MainWindow
* window
)
305 App::_LoadSettings(BMessage
& settings
)
307 if (!fSettingsRead
) {
309 if (load_settings(&fSettings
, "main_settings", "HaikuDepot") != B_OK
)
310 fSettings
.MakeEmpty();
312 settings
= fSettings
;
313 return !fSettings
.IsEmpty();
318 App::_StoreSettings(const BMessage
& settings
)
320 // Take what is in settings and replace data under the same name in
321 // fSettings, leaving anything in fSettings that is not contained in
329 while (settings
.GetInfo(B_ANY_TYPE
, i
++, &name
, &type
, &count
) == B_OK
) {
330 fSettings
.RemoveName(name
);
331 for (int32 j
= 0; j
< count
; j
++) {
334 if (settings
.FindData(name
, type
, j
, &data
, &size
) != B_OK
)
336 fSettings
.AddData(name
, type
, data
, size
);
340 save_settings(&fSettings
, "main_settings", "HaikuDepot");
347 static const char* kPackageDaemonSignature
348 = "application/x-vnd.haiku-package_daemon";
351 App::_CheckPackageDaemonRuns()
353 while (!be_roster
->IsRunning(kPackageDaemonSignature
)) {
354 BAlert
* alert
= new BAlert("start_package_daemon",
355 B_TRANSLATE("HaikuDepot needs the package daemon to function, "
356 "and it appears to be not running.\n"
357 "Would you like to start it now?"),
358 B_TRANSLATE("No, quit HaikuDepot"),
359 B_TRANSLATE("Start package daemon"), NULL
, B_WIDTH_AS_USUAL
,
361 alert
->SetShortcut(0, B_ESCAPE
);
363 if (alert
->Go() == 0)
366 if (!_LaunchPackageDaemon())
373 App::_LaunchPackageDaemon()
375 status_t ret
= be_roster
->Launch(kPackageDaemonSignature
);
378 = B_TRANSLATE("Starting the package daemon failed:\n\n%Error%");
379 errorMessage
.ReplaceAll("%Error%", strerror(ret
));
381 BAlert
* alert
= new BAlert("package_daemon_problem",
383 B_TRANSLATE("Quit HaikuDepot"),
384 B_TRANSLATE("Try again"), NULL
, B_WIDTH_AS_USUAL
,
386 alert
->SetShortcut(0, B_ESCAPE
);
388 if (alert
->Go() == 0)
391 // TODO: Would be nice to send a message to the package daemon instead
392 // and get a reply once it is ready.