xref: /haiku/src/apps/softwareupdater/SoftwareUpdaterApp.cpp (revision 73c2c7b4b5044a152e9d194364578c1c9a13d8c1)
1 /*
2  * Copyright 2016-2017 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT license
4  *
5  * Authors:
6  *		Alexander von Gluck IV <kallisti5@unixzen.com>
7  *		Brian Hill <supernova@warpmail.net>
8  */
9 
10 #include "SoftwareUpdaterApp.h"
11 
12 #include <Catalog.h>
13 
14 #undef B_TRANSLATION_CONTEXT
15 #define B_TRANSLATION_CONTEXT "SoftwareUpdaterApp"
16 
17 
18 static const char* const kUsage = B_TRANSLATE_COMMENT(
19 	"Usage:  SoftwareUpdater <command>\n"
20 	"Updates installed packages.\n"
21 	"\n"
22 	"Commands:\n"
23 	"  update     - Search repositories for updates on all packages.\n"
24 	"  check      - Check for available updates but only display a "
25 		"notification with results.\n"
26 	"  full-sync  - Synchronize the installed packages with the "
27 		"repositories.\n", "Command line usage help")
28 ;
29 
30 
31 SoftwareUpdaterApp::SoftwareUpdaterApp()
32 	:
33 	BApplication(kAppSignature),
34 	fWorker(NULL),
35 	fFinalQuitFlag(false),
36 	fActionRequested(UPDATE)
37 {
38 }
39 
40 
41 SoftwareUpdaterApp::~SoftwareUpdaterApp()
42 {
43 	if (fWorker) {
44 		fWorker->Lock();
45 		fWorker->Quit();
46 	}
47 }
48 
49 
50 bool
51 SoftwareUpdaterApp::QuitRequested()
52 {
53 	if (fFinalQuitFlag)
54 		return true;
55 
56 	// Simulate a cancel request from window- this gives the updater a chance
57 	// to quit cleanly
58 	if (fWindowMessenger.IsValid()) {
59 		fWindowMessenger.SendMessage(kMsgCancel);
60 		return false;
61 	}
62 
63 	return true;
64 }
65 
66 
67 void
68 SoftwareUpdaterApp::ReadyToRun()
69 {
70 	fWorker = new WorkingLooper(fActionRequested);
71 }
72 
73 
74 void
75 SoftwareUpdaterApp::ArgvReceived(int32 argc, char **argv)
76 {
77 	// Only one command allowed
78 	if (argc > 2) {
79 		fprintf(stderr, kUsage);
80 		PostMessage(B_QUIT_REQUESTED);
81 		return;
82 	} else if (argc == 2) {
83 		fActionRequested = USER_SELECTION_NEEDED;
84 		const char* command = argv[1];
85 		if (strcmp("update", command) == 0)
86 			fActionRequested = UPDATE;
87 		else if (strcmp("check", command) == 0)
88 			fActionRequested = UPDATE_CHECK_ONLY;
89 		else if (strcmp("full-sync", command) == 0)
90 			fActionRequested = FULLSYNC;
91 		else
92 			fprintf(stderr, kUsage);
93 	}
94 }
95 
96 
97 void
98 SoftwareUpdaterApp::MessageReceived(BMessage* message)
99 {
100 	switch (message->what) {
101 		case kMsgRegister:
102 			message->FindMessenger(kKeyMessenger, &fWindowMessenger);
103 			break;
104 
105 		case kMsgFinalQuit:
106 			fFinalQuitFlag = true;
107 			PostMessage(B_QUIT_REQUESTED);
108 			break;
109 
110 		default:
111 			BApplication::MessageReceived(message);
112 	}
113 }
114 
115 
116 int
117 main(int argc, const char* const* argv)
118 {
119 	if (argc > 2) {
120 		fprintf(stderr, kUsage);
121 		return 1;
122 	}
123 
124 	SoftwareUpdaterApp app;
125 	return app.Run();
126 }
127