xref: /haiku/src/apps/softwareupdater/SoftwareUpdaterApp.cpp (revision 6889394848e2dc9f41ff53b12141d572822ca0c6)
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 <getopt.h>
13 #include <stdlib.h>
14 
15 #include <Catalog.h>
16 
17 #undef B_TRANSLATION_CONTEXT
18 #define B_TRANSLATION_CONTEXT "SoftwareUpdaterApp"
19 
20 
21 static const char* const kUsage = B_TRANSLATE_COMMENT(
22 	"Usage:  SoftwareUpdater <command> [ <option> ]\n"
23 	"Updates installed packages.\n"
24 	"\n"
25 	"Commands:\n"
26 	"  update     - Search repositories for updates on all packages.\n"
27 	"  check      - Check for available updates but only display a "
28 		"notification with results.\n"
29 	"  full-sync  - Synchronize the installed packages with the "
30 		"repositories.\n"
31 	"\n"
32 	"Options:\n"
33 	"  -h or --help       Print this usage help\n"
34 	"  -v or --verbose    Output verbose information\n",
35 	"Command line usage help")
36 ;
37 
38 static struct option const kLongOptions[] = {
39 	{"verbose", no_argument, 0, 'v'},
40 	{"help", no_argument, 0, 'h'},
41 	{NULL}
42 };
43 
44 
45 SoftwareUpdaterApp::SoftwareUpdaterApp()
46 	:
47 	BApplication(kAppSignature),
48 	fWorker(NULL),
49 	fFinalQuitFlag(false),
50 	fActionRequested(UPDATE),
51 	fVerbose(false),
52 	fArgvsAccepted(true)
53 {
54 }
55 
56 
57 SoftwareUpdaterApp::~SoftwareUpdaterApp()
58 {
59 	if (fWorker) {
60 		fWorker->Lock();
61 		fWorker->Quit();
62 	}
63 }
64 
65 
66 bool
67 SoftwareUpdaterApp::QuitRequested()
68 {
69 	if (fFinalQuitFlag)
70 		return true;
71 
72 	// Simulate a cancel request from window- this gives the updater a chance
73 	// to quit cleanly
74 	if (fWindowMessenger.IsValid()) {
75 		fWindowMessenger.SendMessage(kMsgCancel);
76 		return false;
77 	}
78 
79 	return true;
80 }
81 
82 
83 void
84 SoftwareUpdaterApp::ReadyToRun()
85 {
86 	// Argvs no longer accepted once the process starts
87 	fArgvsAccepted = false;
88 
89 	fWorker = new WorkingLooper(fActionRequested, fVerbose);
90 }
91 
92 
93 void
94 SoftwareUpdaterApp::ArgvReceived(int32 argc, char **argv)
95 {
96 	if (!fArgvsAccepted) {
97 		fputs(B_TRANSLATE("Argument variables are no longer accepted\n"),
98 			stderr);
99 		return;
100 	}
101 
102 	int c;
103 	while ((c = getopt_long(argc, argv, "hv", kLongOptions, NULL)) != -1) {
104 		switch (c) {
105 			case 0:
106 				break;
107 			case 'h':
108 				fputs(kUsage, stdout);
109 				exit(0);
110 				break;
111 			case 'v':
112 				fVerbose = true;
113 				break;
114 			default:
115 				fputs(kUsage, stderr);
116 				exit(1);
117 				break;
118 		}
119 	}
120 
121 	const char* command = "";
122 	int32 argCount = argc - optind;
123 	if (argCount == 0)
124 		return;
125 	else if (argCount > 1) {
126 		fputs(kUsage, stderr);
127 		exit(1);
128 	} else
129 		command = argv[optind];
130 
131 	fActionRequested = USER_SELECTION_NEEDED;
132 	if (strcmp("update", command) == 0)
133 		fActionRequested = UPDATE;
134 	else if (strcmp("check", command) == 0)
135 		fActionRequested = UPDATE_CHECK_ONLY;
136 	else if (strcmp("full-sync", command) == 0)
137 		fActionRequested = FULLSYNC;
138 	else {
139 		fputs(B_TRANSLATE_COMMENT("Unrecognized argument", "Error message"),
140 			stderr);
141 		fprintf(stderr, " \"%s\"\n", command);
142 		fputs(kUsage, stderr);
143 	}
144 }
145 
146 
147 void
148 SoftwareUpdaterApp::MessageReceived(BMessage* message)
149 {
150 	switch (message->what) {
151 		case kMsgRegister:
152 			message->FindMessenger(kKeyMessenger, &fWindowMessenger);
153 			break;
154 
155 		case kMsgFinalQuit:
156 			fFinalQuitFlag = true;
157 			PostMessage(B_QUIT_REQUESTED);
158 			break;
159 
160 		default:
161 			BApplication::MessageReceived(message);
162 	}
163 }
164 
165 
166 int
167 main(int argc, const char* const* argv)
168 {
169 	SoftwareUpdaterApp app;
170 	return app.Run();
171 }
172