summaryrefslogtreecommitdiff
path: root/src/main.c
blob: ec01b419ccce764a97debff4f4c511b61a60be7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* vim: colorcolumn=80 ts=4 sw=4
 */
/*
 * main.c
 *
 * Copyright © 2002 Sun Microsystems, Inc.
 * Copyright © 2021-2023 Logan Rathbone
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Original Author: Glynn Foster <glynn.foster@sun.com>
 */

#include "option.h"
#include "zenity.h"

#include <adwaita.h>
#include <locale.h>
#include <stdlib.h>

#include <config.h>

typedef struct {
	int argc;
	char **argv;
} ZenityArgs;

static void
command_line_cb (GApplication *app,
               GApplicationCommandLine *command_line,
               gpointer user_data)
{
	ZenityArgs *args = user_data;
	ZenityParsingOptions *results;

	results = zenity_option_parse (args->argc, args->argv);

	switch (results->mode)
	{
		case MODE_CALENDAR:
			zenity_calendar (results->data, results->calendar_data);
			break;

		case MODE_ENTRY:
			/* allow for a series of tokens (or even a bash array!) to be
			 * passed as arguments so as to auto-populate the entry with
			 * a list of options as a combo-box.
			 */
			results->entry_data->data = (const char **) args->argv + 1;
			zenity_entry (results->data, results->entry_data);
			break;

		case MODE_ERROR:
		case MODE_QUESTION:
		case MODE_WARNING:
		case MODE_INFO:
			zenity_msg (results->data, results->msg_data);
			break;

		case MODE_SCALE:
			zenity_scale (results->data, results->scale_data);
			break;

		case MODE_FILE:
			zenity_fileselection (results->data, results->file_data);
			break;

		case MODE_LIST:
			results->tree_data->data = (const char **) args->argv + 1;
			zenity_tree (results->data, results->tree_data);
			break;
			
		case MODE_NOTIFICATION:
			zenity_notification (results->data, results->notification_data);
			break;

		case MODE_PROGRESS:
			zenity_progress (results->data, results->progress_data);
			break;

		case MODE_TEXTINFO:
			zenity_text (results->data, results->text_data);
			break;

		case MODE_COLOR:
			zenity_colorselection (results->data, results->color_data);
			break;

		case MODE_PASSWORD:
			zenity_password_dialog (results->data, results->password_data);
			break;

		case MODE_ABOUT:
			zenity_about (results->data);
			break;

		case MODE_FORMS:
			zenity_forms_dialog (results->data, results->forms_data);
			break;

		case MODE_VERSION:
			g_print ("%s\n", VERSION);
			break;

		case MODE_LAST:
			g_printerr (_ ("You must specify a dialog type. See 'zenity "
						   "--help' for details\n"));
			exit (-1);

		default:
			g_assert_not_reached ();
			exit (-1);
	}

	g_free (args);
}

static void dummy_log_func (void) { }

int
main (int argc, char *argv[])
{
	ZenityArgs *args;
	g_autoptr(AdwApplication) app = NULL;
	int status;

	/* <i18n> */
	setlocale (LC_ALL, "");
	bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
                                                                                
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);
	/* </i18n> */

	/* Turn off g_message's from libadwaita - this is to suppress the 'this is
	 * discouraged' message re: mapping dialogs without a transient parent.
	 */
	g_log_set_handler ("Adwaita", G_LOG_LEVEL_MESSAGE, (GLogFunc)dummy_log_func, NULL);

	args = g_new0 (ZenityArgs, 1);
	args->argc = argc;
	args->argv = argv;

	app = adw_application_new (APP_ID, G_APPLICATION_HANDLES_COMMAND_LINE);
	g_signal_connect (app, "command-line", G_CALLBACK(command_line_cb), args);

	status = g_application_run (G_APPLICATION(app), 0, NULL);

	return status;
}