Freeciv-3.3
Loading...
Searching...
No Matches
mpgui_gtk5.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <stdlib.h>
19
20#include <gtk/gtk.h>
21
22/* utility */
23#include "fc_cmdline.h"
24#include "fciconv.h"
25#include "fcintl.h"
26#include "log.h"
27#include "mem.h"
28
29/* common */
30#include "version.h"
31
32/* modinst */
33#include "download.h"
34#include "mpcmdline.h"
35#include "mpdb.h"
36
37#include "modinst.h"
38
47
48struct fcmp_params fcmp = {
50 .inst_prefix = nullptr,
51 .autoinstall = nullptr
52};
53
55
57
58#define ML_COL_NAME 0
59#define ML_COL_VER 1
60#define ML_COL_INST 2
61#define ML_COL_TYPE 3
62#define ML_COL_SUBTYPE 4
63#define ML_COL_LIC 5
64#define ML_COL_URL 6
65
66#define ML_COL_COUNT 7
67
68#define ML_TYPE 7
69#define ML_NOTES 8
70#define ML_STORE_SIZE 9
71
72/**********************************************************************/
75static void modinst_quit(void)
76{
78}
79
80/**********************************************************************/
84static void quit_dialog_response(GObject *dialog, GAsyncResult *result,
85 gpointer data)
86{
88 result, nullptr);
89
90 if (button == 0) {
92 }
93
94 quit_dialog = nullptr;
95}
96
97/**********************************************************************/
101{
102 if (downloading || TRUE) {
103 /* Download in progress. Confirm quit from user. */
104
105 if (quit_dialog == nullptr) {
106 const char *buttons[] = { _("Quit"), _("Cancel"), nullptr };
107
108 quit_dialog = gtk_alert_dialog_new(_("Modpack installation in progress.\n"
109 "Are you sure you want to quit?"));
110
112
114 GTK_WINDOW(toplevel), nullptr,
115 quit_dialog_response, nullptr);
116 }
117
118 } else {
119 /* User loses no work by quitting, so let's not annoy them
120 * with confirmation dialog. */
121 modinst_quit();
122 }
123
124 /* Stop emission of event. */
125 return TRUE;
126}
127
128/**********************************************************************/
131static void msg_callback(const char *msg)
132{
133 log_verbose("%s", msg);
135}
136
137/**********************************************************************/
140static void pbar_callback(int downloaded, int max)
141{
142 /* Control file already downloaded */
143 double fraction = (double) downloaded / (max);
144
146}
147
148struct msg_data {
149 char *msg;
150};
151
152/**********************************************************************/
156{
157 struct msg_data *data = (struct msg_data *)user_data;
158
159 msg_callback(data->msg);
160
161 FC_FREE(data->msg);
162 FC_FREE(data);
163
164 return G_SOURCE_REMOVE;
165}
166
167/**********************************************************************/
170static void msg_dl_thread(const char *msg)
171{
172 struct msg_data *data = fc_malloc(sizeof(*data));
173
174 data->msg = fc_strdup(msg);
175
177}
178
179struct pbar_data {
180 int current;
181 int max;
182};
183
184/**********************************************************************/
188{
189 struct pbar_data *data = (struct pbar_data *)user_data;
190
191 pbar_callback(data->current, data->max);
192
193 FC_FREE(data);
194
195 return G_SOURCE_REMOVE;
196}
197
198/**********************************************************************/
201static void pbar_dl_thread(int current, int max)
202{
203 struct pbar_data *data = fc_malloc(sizeof(*data));
204
205 data->current = current;
206 data->max = max;
207
209}
210
211/**********************************************************************/
215{
217
219 do {
220 const char *name_str;
221 int type_int;
222 const char *new_inst;
223 enum modpack_type type;
224
228 -1);
229
230 type = type_int;
231
233
234 if (new_inst == nullptr) {
235 new_inst = _("Not installed");
236 }
237
240 -1);
241
243 }
244
245 return G_SOURCE_REMOVE;
246}
247
248/**********************************************************************/
255
256/**********************************************************************/
260{
261 const char *errmsg;
262
264
265 if (errmsg == nullptr) {
266 msg_dl_thread(_("Ready"));
267 } else {
269 }
270
271 free(data);
272
274
276
277 return nullptr;
278}
279
280/**********************************************************************/
283static void gui_download_modpack(const char *URL)
284{
286 char *URLbuf;
287
288 if (downloading) {
290 _("Another download already active"));
291 return;
292 }
293
295
296 URLbuf = fc_malloc(strlen(URL) + 1);
297
298 strcpy(URLbuf, URL);
299
301 if (downloader == nullptr) {
303 _("Failed to start downloader"));
304 free(URLbuf);
305 } else {
307 }
308}
309
310/**********************************************************************/
313static void install_clicked(GtkWidget *w, gpointer data)
314{
315 GtkEntry *URL_in = data;
317 const char *URL = gtk_entry_buffer_get_text(buffer);
318
320}
321
322/**********************************************************************/
325static void URL_return(GtkEntry *w, gpointer data)
326{
327 const char *URL;
329
330 URL = gtk_entry_buffer_get_text(buffer);
332}
333
334/**********************************************************************/
338 gint x, gint y,
341 gpointer data)
342{
344 GtkTreeView *tree_view = GTK_TREE_VIEW(widget);
345 GtkTreeModel *model;
346 const char *notes;
347
348 if (!gtk_tree_view_get_tooltip_context(tree_view, x, y,
350 &model, nullptr, &iter)) {
351 return FALSE;
352 }
353
354 gtk_tree_model_get(model, &iter,
355 ML_NOTES, &notes,
356 -1);
357
358 if (notes != nullptr) {
360
361 return TRUE;
362 }
363
364 return FALSE;
365}
366
367/**********************************************************************/
370static void setup_modpack_list(const char *name, const char *URL,
371 const char *version, const char *license,
372 enum modpack_type type, const char *subtype,
373 const char *notes)
374{
376 const char *type_str;
377 const char *lic_str;
378 const char *inst_str;
379
382 } else {
383 /* TRANS: Unknown modpack type */
384 type_str = _("?");
385 }
386
387 if (license != nullptr) {
389 } else {
390 /* TRANS: License of modpack is not known */
391 lic_str = Q_("?license:Unknown");
392 }
393
395 if (inst_str == nullptr) {
396 inst_str = _("Not installed");
397 }
398
402 ML_COL_VER, version,
407 ML_COL_URL, URL,
408 ML_TYPE, type,
410 -1);
411}
412
413/**********************************************************************/
417{
418 GtkTreeModel *model;
419 GtkTreeIter it;
420 const char *URL;
421 GtkEntryBuffer *buffer;
422
423 if (!gtk_tree_selection_get_selected(select, &model, &it)) {
424 return;
425 }
426
427 gtk_tree_model_get(model, &it, ML_COL_URL, &URL, -1);
428
430 gtk_entry_buffer_set_text(buffer, URL, -1);
431}
432
433/**********************************************************************/
436static void modinst_setup_widgets(void)
437{
439 GtkWidget *version_label;
442 GtkCellRenderer *renderer;
443 GtkTreeSelection *selection;
444 const char *errmsg;
445 char verbuf[2048];
446 const char *rev_ver;
447
448 mbox = gtk_grid_new();
452
454
455 if (rev_ver == nullptr) {
457 } else {
458 fc_snprintf(verbuf, sizeof(verbuf), _("%s%s\ncommit: %s"),
460 }
461
462 version_label = gtk_label_new(verbuf);
463
465 renderer = gtk_cell_renderer_text_new();
468 _("Name"), renderer, "text", 0,
469 nullptr);
470 renderer = gtk_cell_renderer_text_new();
473 _("Version"), renderer, "text", 1,
474 nullptr);
475 renderer = gtk_cell_renderer_text_new();
478 _("Installed"), renderer, "text", 2,
479 nullptr);
480 renderer = gtk_cell_renderer_text_new();
483 Q_("?modpack:Type"),
484 renderer, "text", 3,
485 nullptr);
486 renderer = gtk_cell_renderer_text_new();
489 _("Subtype"),
490 renderer, "text", 4,
491 nullptr);
492 renderer = gtk_cell_renderer_text_new();
495 /* TRANS: noun */
496 _("License"), renderer, "text", 5,
497 nullptr);
498 renderer = gtk_cell_renderer_text_new();
501 _("URL"), renderer, "text", 6,
502 nullptr);
504 g_signal_connect(selection, "changed", G_CALLBACK(select_from_list), nullptr);
505
507 gtk_button_set_label(GTK_BUTTON(install_button), _("Install modpack"));
508
509 Ubox = gtk_grid_new();
512 URL_label = gtk_label_new_with_mnemonic(_("Modpack URL"));
513
517 g_signal_connect(URL_input, "activate",
518 G_CALLBACK(URL_return), nullptr);
519
522
523 gtk_grid_attach(GTK_GRID(Ubox), URL_label, 0, 0, 1, 1);
524 gtk_grid_attach(GTK_GRID(Ubox), URL_input, 0, 1, 1, 1);
525
527
528 statusbar = gtk_label_new(_("Select modpack to install"));
529
532
533 gtk_grid_attach(GTK_GRID(mbox), version_label, 0, 0, 1, 1);
534 gtk_grid_attach(GTK_GRID(mbox), main_list, 0, 1, 1, 1);
535 gtk_grid_attach(GTK_GRID(mbox), Ubox, 0, 2, 1, 1);
538 gtk_grid_attach(GTK_GRID(mbox), statusbar, 0, 5, 1, 1);
539
541
548
549 g_object_set(main_list, "has-tooltip", TRUE, nullptr);
550 g_signal_connect(main_list, "query-tooltip",
552
554
555 if (errmsg != nullptr) {
557 }
558}
559
560/**********************************************************************/
564{
565 quit_dialog = nullptr;
566
568
570 gtk_widget_set_name(toplevel, "Freeciv-modpack");
572 _("Freeciv modpack installer (gtk4x)"));
573
574#if 0
575 /* Keep the icon of the executable on Windows */
576#ifndef FREECIV_MSWINDOWS
577 {
578 /* Unlike main client, this only works if installed. Ignore any
579 * errors loading the icon. */
580 GError *err;
581
583 MPICON_PATH, &err);
584 }
585#endif /* FREECIV_MSWINDOWS */
586#endif /* 0 */
587
588 g_signal_connect(toplevel, "close_request",
590
592
594
595 if (fcmp.autoinstall != nullptr) {
597 }
598}
599
600/**********************************************************************/
603int main(int argc, char *argv[])
604{
605 int ui_options;
606
607 fcmp_init();
608
609 /* This modifies argv! */
611
612 if (ui_options != -1) {
613 int i;
614
615 for (i = 1; i <= ui_options; i++) {
616 if (is_option("--help", argv[i])) {
617 /* TRANS: No full stop after the URL, could cause confusion. */
618 fc_fprintf(stderr, _("Report bugs at %s\n"), BUG_URL);
619
620 ui_options = -1;
621 }
622 }
623 }
624
625 if (ui_options != -1) {
627
628 if (gtk_init_check()) {
629 fcmp_app = gtk_application_new(nullptr, 0);
630 g_signal_connect(fcmp_app, "activate",
631 G_CALLBACK(activate_gui), nullptr);
633
635 } else {
636 log_fatal(_("Failed to open graphical mode."));
637 }
638
639 close_mpdbs();
640 }
641
642 fcmp_deinit();
644
645 return EXIT_SUCCESS;
646}
char * incite_cost
Definition comments.c:74
const char * download_modpack(const char *URL, const struct fcmp_params *fcmp, dl_msg_callback mcb, dl_pb_callback pbcb)
Definition download.c:66
const char * download_modpack_list(const struct fcmp_params *fcmp, modpack_list_setup_cb cb, dl_msg_callback mcb)
Definition download.c:427
bool is_option(const char *option_name, char *option)
Definition fc_cmdline.c:112
void cmdline_option_values_free(void)
Definition fc_cmdline.c:97
void fc_fprintf(FILE *stream, const char *format,...) fc__attribute((__format__(__printf__
#define Q_(String)
Definition fcintl.h:70
#define _(String)
Definition fcintl.h:67
const char * tooltip
Definition repodlgs.c:1315
GType type
Definition repodlgs.c:1313
const char * name
Definition inputfile.c:127
#define log_verbose(message,...)
Definition log.h:109
#define log_fatal(message,...)
Definition log.h:100
#define FC_FREE(ptr)
Definition mem.h:41
#define fc_strdup(str)
Definition mem.h:43
#define fc_malloc(sz)
Definition mem.h:34
void load_install_info_lists(struct fcmp_params *fcmp)
Definition modinst.c:44
void fcmp_deinit(void)
Definition modinst.c:98
void fcmp_init(void)
Definition modinst.c:82
#define DEFAULT_URL_START
Definition modinst.h:32
#define MODPACK_LIST_URL
Definition modinst.h:30
int fcmp_parse_cmdline(int argc, char *argv[])
Definition mpcmdline.c:45
const char * mpdb_installed_version(const char *name, enum modpack_type type)
Definition mpdb.c:253
void close_mpdbs(void)
Definition mpdb.c:202
#define ML_COL_SUBTYPE
Definition mpgui_gtk5.c:62
static void msg_dl_thread(const char *msg)
Definition mpgui_gtk5.c:170
#define ML_TYPE
Definition mpgui_gtk5.c:68
int main(int argc, char *argv[])
Definition mpgui_gtk5.c:603
#define ML_NOTES
Definition mpgui_gtk5.c:69
static gboolean quit_dialog_callback(void)
Definition mpgui_gtk5.c:100
static GtkWidget * statusbar
Definition mpgui_gtk5.c:40
static gboolean query_main_list_tooltip_cb(GtkWidget *widget, gint x, gint y, gboolean keyboard_tip, GtkTooltip *tooltip, gpointer data)
Definition mpgui_gtk5.c:337
static void select_from_list(GtkTreeSelection *select, gpointer data)
Definition mpgui_gtk5.c:416
#define ML_STORE_SIZE
Definition mpgui_gtk5.c:70
static GtkListStore * main_store
Definition mpgui_gtk5.c:43
static gboolean versionlist_update_main_thread(gpointer user_data)
Definition mpgui_gtk5.c:214
static void activate_gui(GtkApplication *app, gpointer data)
Definition mpgui_gtk5.c:563
#define ML_COL_NAME
Definition mpgui_gtk5.c:58
static GtkWidget * toplevel
Definition mpgui_gtk5.c:39
static void modinst_quit(void)
Definition mpgui_gtk5.c:75
#define ML_COL_LIC
Definition mpgui_gtk5.c:63
static void pbar_callback(int downloaded, int max)
Definition mpgui_gtk5.c:140
static void URL_return(GtkEntry *w, gpointer data)
Definition mpgui_gtk5.c:325
static GtkWidget * URL_input
Definition mpgui_gtk5.c:44
struct fcmp_params fcmp
Definition mpgui_gtk5.c:48
static gboolean downloading
Definition mpgui_gtk5.c:46
static GtkWidget * progressbar
Definition mpgui_gtk5.c:41
static void gui_download_modpack(const char *URL)
Definition mpgui_gtk5.c:283
static gboolean msg_main_thread(gpointer user_data)
Definition mpgui_gtk5.c:155
static void install_clicked(GtkWidget *w, gpointer data)
Definition mpgui_gtk5.c:313
static GtkAlertDialog * quit_dialog
Definition mpgui_gtk5.c:45
#define ML_COL_TYPE
Definition mpgui_gtk5.c:61
static void setup_modpack_list(const char *name, const char *URL, const char *version, const char *license, enum modpack_type type, const char *subtype, const char *notes)
Definition mpgui_gtk5.c:370
static GtkApplication * fcmp_app
Definition mpgui_gtk5.c:54
static void pbar_dl_thread(int current, int max)
Definition mpgui_gtk5.c:201
static void msg_callback(const char *msg)
Definition mpgui_gtk5.c:131
#define ML_COL_INST
Definition mpgui_gtk5.c:60
static GtkWidget * main_list
Definition mpgui_gtk5.c:42
static void quit_dialog_response(GObject *dialog, GAsyncResult *result, gpointer data)
Definition mpgui_gtk5.c:84
static void modinst_setup_widgets(void)
Definition mpgui_gtk5.c:436
static void versionlist_update_dl_thread(void)
Definition mpgui_gtk5.c:251
#define ML_COL_URL
Definition mpgui_gtk5.c:64
static gboolean pbar_main_thread(gpointer user_data)
Definition mpgui_gtk5.c:187
#define ML_COL_VER
Definition mpgui_gtk5.c:59
static gpointer download_thread(gpointer data)
Definition mpgui_gtk5.c:259
struct sprite int int y
Definition sprite_g.h:31
struct sprite int x
Definition sprite_g.h:31
const char * list_url
Definition modinst.h:18
const char * autoinstall
Definition modinst.h:20
char * msg
Definition mpgui_gtk3.c:145
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
const char * fc_git_revision(void)
Definition version.c:75
const char * word_version(void)
Definition version.c:62