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#define FC_TYPE_MPROW (fc_mprow_get_type())
73
75
77{
79
80 const char *name;
81 const char *ver;
82 const char *inst;
83 const char *type;
84 const char *subtype;
85 const char *lic;
86 char *URL;
87 const char *notes;
88
90};
91
96
98
99/**********************************************************************/
103{
105
106 free(row->URL);
107 row->URL = nullptr;
108
110}
111
112/**********************************************************************/
115static void
122
123/**********************************************************************/
126static void
128{
129 self->URL = nullptr;
130}
131
132/**********************************************************************/
136{
137 FcMPRow *result;
138
139 result = g_object_new(FC_TYPE_MPROW, nullptr);
140
141 return result;
142}
143
144/**********************************************************************/
147static void modinst_quit(void)
148{
150}
151
152/**********************************************************************/
156static void quit_dialog_response(GObject *dialog, GAsyncResult *result,
157 gpointer data)
158{
160 result, nullptr);
161
162 if (button == 0) {
163 modinst_quit();
164 }
165
166 quit_dialog = nullptr;
167}
168
169/**********************************************************************/
173{
174 if (downloading) {
175 /* Download in progress. Confirm quit from user. */
176
177 if (quit_dialog == nullptr) {
178 const char *buttons[] = { _("Quit"), _("Cancel"), nullptr };
179
180 quit_dialog = gtk_alert_dialog_new(_("Modpack installation in progress.\n"
181 "Are you sure you want to quit?"));
182
184
186 GTK_WINDOW(toplevel), nullptr,
187 quit_dialog_response, nullptr);
188 }
189
190 } else {
191 /* User loses no work by quitting, so let's not annoy them
192 * with confirmation dialog. */
193 modinst_quit();
194 }
195
196 /* Stop emission of event. */
197 return TRUE;
198}
199
200/**********************************************************************/
203static void msg_callback(const char *msg)
204{
205 log_verbose("%s", msg);
207}
208
209/**********************************************************************/
212static void pbar_callback(int downloaded, int max)
213{
214 /* Control file already downloaded */
215 double fraction = (double) downloaded / (max);
216
218}
219
220struct msg_data {
221 char *msg;
222};
223
224/**********************************************************************/
228{
229 struct msg_data *data = (struct msg_data *)user_data;
230
231 msg_callback(data->msg);
232
233 FC_FREE(data->msg);
234 FC_FREE(data);
235
236 return G_SOURCE_REMOVE;
237}
238
239/**********************************************************************/
242static void msg_dl_thread(const char *msg)
243{
244 struct msg_data *data = fc_malloc(sizeof(*data));
245
246 data->msg = fc_strdup(msg);
247
249}
250
251struct pbar_data {
252 int current;
253 int max;
254};
255
256/**********************************************************************/
260{
261 struct pbar_data *data = (struct pbar_data *)user_data;
262
263 pbar_callback(data->current, data->max);
264
265 FC_FREE(data);
266
267 return G_SOURCE_REMOVE;
268}
269
270/**********************************************************************/
273static void pbar_dl_thread(int current, int max)
274{
275 struct pbar_data *data = fc_malloc(sizeof(*data));
276
277 data->current = current;
278 data->max = max;
279
281}
282
283/**********************************************************************/
287{
288#if 0
290
292 do {
293 const char *name_str;
294 int type_int;
295 const char *new_inst;
296 enum modpack_type type;
297
300 ML_TYPE, &type_int,
301 -1);
302
303 type = type_int;
304
306
307 if (new_inst == nullptr) {
308 new_inst = _("Not installed");
309 }
310
313 -1);
314
316 }
317#endif
318
319 return G_SOURCE_REMOVE;
320}
321
322/**********************************************************************/
329
330/**********************************************************************/
334{
335 const char *errmsg;
336
338
339 if (errmsg == nullptr) {
340 msg_dl_thread(_("Ready"));
341 } else {
343 }
344
345 free(data);
346
348
350
351 return nullptr;
352}
353
354/**********************************************************************/
357static void gui_download_modpack(const char *URL)
358{
360 char *URLbuf;
361
362 if (downloading) {
364 _("Another download already active"));
365 return;
366 }
367
369
370 URLbuf = fc_malloc(strlen(URL) + 1);
371
372 strcpy(URLbuf, URL);
373
375 if (downloader == nullptr) {
377 _("Failed to start downloader"));
378 free(URLbuf);
379 } else {
381 }
382}
383
384/**********************************************************************/
387static void install_clicked(GtkWidget *w, gpointer data)
388{
389 GtkEntry *URL_in = data;
391 const char *URL = gtk_entry_buffer_get_text(buffer);
392
394}
395
396/**********************************************************************/
399static void URL_return(GtkEntry *w, gpointer data)
400{
401 const char *URL;
403
404 URL = gtk_entry_buffer_get_text(buffer);
406}
407
408/**********************************************************************/
412 gint x, gint y,
415 gpointer data)
416{
418 int row_number = -1; /* 0 after header */
419 int curr_y = 0;
420
421 while (GTK_IS_WIDGET(child)) {
423
424 if (curr_y > y) {
426
427 if (row != nullptr) {
428 log_debug("Tooltip row %d. Notes: %s", row_number,
429 row->notes == nullptr ? "-" : row->notes);
430
431 if (row->notes != nullptr) {
433
434 return TRUE;
435 }
436 }
437
438 return FALSE;
439 }
440
441 row_number++;
442
443 child = gtk_widget_get_next_sibling(child);
444 }
445
446 return FALSE;
447}
448
449/**********************************************************************/
452static void setup_modpack_list(const char *name, const char *URL,
453 const char *version, const char *license,
454 enum modpack_type type, const char *subtype,
455 const char *notes)
456{
457 FcMPRow *row;
458
459 row = fc_mprow_new();
460
462 row->type = _(modpack_type_name(type));
463 } else {
464 /* TRANS: Unknown modpack type */
465 row->type = _("?");
466 }
467
468 if (license != nullptr) {
469 row->lic = license;
470 } else {
471 /* TRANS: License of modpack is not known */
472 row->lic = Q_("?license:Unknown");
473 }
474
476 if (row->inst == nullptr) {
477 row->inst = _("Not installed");
478 }
479
480 row->name = name;
481 row->ver = version;
482 row->subtype = subtype;
483 row->URL = fc_strdup(URL);
484 row->notes = notes;
485
486 row->type_int = type;
487
490}
491
492/**********************************************************************/
496 guint position, guint n_items,
497 gpointer user_data)
498{
499 GtkSingleSelection *selection = GTK_SINGLE_SELECTION(model);
503 GtkEntryBuffer *buffer;
504
505 log_debug("Selected row: %d. URL: %s", row_number, row->URL);
506
508 gtk_entry_buffer_set_text(buffer, row->URL, -1);
509}
510
511/**********************************************************************/
516 gpointer user_data)
517{
518 FcMPRow *row;
519 const char *str = "-";
520
522
523 switch (GPOINTER_TO_INT(user_data)) {
524 case ML_COL_NAME:
525 str = row->name;
526 break;
527 case ML_COL_VER:
528 str = row->ver;
529 break;
530 case ML_COL_INST:
531 str = row->inst;
532 break;
533 case ML_COL_TYPE:
534 str = row->type;
535 break;
536 case ML_COL_SUBTYPE:
537 str = row->subtype;
538 break;
539 case ML_COL_LIC:
540 str = row->lic;
541 break;
542 case ML_COL_URL:
543 str = row->URL;
544 break;
545 }
546
548 str);
549}
550
551/**********************************************************************/
560
561/**********************************************************************/
564static void modinst_setup_widgets(void)
565{
567 GtkWidget *version_label;
572 GtkSingleSelection *selection;
573 const char *errmsg;
574 char verbuf[2048];
575 const char *rev_ver;
576
577 mbox = gtk_grid_new();
581
583
584 if (rev_ver == nullptr) {
586 } else {
587 fc_snprintf(verbuf, sizeof(verbuf), _("%s%s\ncommit: %s"),
589 }
590
591 version_label = gtk_label_new(verbuf);
592
595 g_signal_connect(selection, "selection-changed", G_CALLBACK(selection_change),
596 nullptr);
597
599
604
607
612
615
620
623
628
629 column = gtk_column_view_column_new(Q_("?modpack:Type"), factory);
631
636
639
644
647
652
655
657 gtk_button_set_label(GTK_BUTTON(install_button), _("Install modpack"));
658
659 Ubox = gtk_grid_new();
662 URL_label = gtk_label_new_with_mnemonic(_("Modpack URL"));
663
667 g_signal_connect(URL_input, "activate",
668 G_CALLBACK(URL_return), nullptr);
669
672
673 gtk_grid_attach(GTK_GRID(Ubox), URL_label, 0, 0, 1, 1);
674 gtk_grid_attach(GTK_GRID(Ubox), URL_input, 0, 1, 1, 1);
675
677
678 statusbar = gtk_label_new(_("Select modpack to install"));
679
682
683 gtk_grid_attach(GTK_GRID(mbox), version_label, 0, 0, 1, 1);
684 gtk_grid_attach(GTK_GRID(mbox), main_list, 0, 1, 1, 1);
685 gtk_grid_attach(GTK_GRID(mbox), Ubox, 0, 2, 1, 1);
688 gtk_grid_attach(GTK_GRID(mbox), statusbar, 0, 5, 1, 1);
689
691
693
694 g_object_set(main_list, "has-tooltip", TRUE, nullptr);
695 g_signal_connect(main_list, "query-tooltip",
697
698 if (errmsg != nullptr) {
700 }
701}
702
703/**********************************************************************/
707{
708 quit_dialog = nullptr;
709
711
713 gtk_widget_set_name(toplevel, "Freeciv-modpack");
715 _("Freeciv modpack installer (gtk4x)"));
716
717#if 0
718 /* Keep the icon of the executable on Windows */
719#ifndef FREECIV_MSWINDOWS
720 {
721 /* Unlike main client, this only works if installed. Ignore any
722 * errors loading the icon. */
723 GError *err;
724
726 MPICON_PATH, &err);
727 }
728#endif /* FREECIV_MSWINDOWS */
729#endif /* 0 */
730
731 g_signal_connect(toplevel, "close_request",
733
735
737
738 if (fcmp.autoinstall != nullptr) {
740 }
741}
742
743/**********************************************************************/
746int main(int argc, char *argv[])
747{
748 int ui_options;
749
750 fcmp_init();
751
752 /* This modifies argv! */
754
755 if (ui_options != -1) {
756 int i;
757
758 for (i = 1; i <= ui_options; i++) {
759 if (is_option("--help", argv[i])) {
760 /* TRANS: No full stop after the URL, could cause confusion. */
761 fc_fprintf(stderr, _("Report bugs at %s\n"), BUG_URL);
762
763 ui_options = -1;
764 }
765 }
766 }
767
768 if (ui_options != -1) {
770
771 if (gtk_init_check()) {
772 fcmp_app = gtk_application_new(nullptr, 0);
773 g_signal_connect(fcmp_app, "activate",
774 G_CALLBACK(activate_gui), nullptr);
776
778 } else {
779 log_fatal(_("Failed to open graphical mode."));
780 }
781
782 close_mpdbs();
783 }
784
785 fcmp_deinit();
787
788 return EXIT_SUCCESS;
789}
#define str
Definition astring.c:76
static struct ai_type * self
Definition classicai.c:46
char * incite_cost
Definition comments.c:76
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:110
#define log_fatal(message,...)
Definition log.h:101
#define log_debug(message,...)
Definition log.h:116
#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:242
#define ML_TYPE
Definition mpgui_gtk5.c:68
int main(int argc, char *argv[])
Definition mpgui_gtk5.c:746
static gboolean quit_dialog_callback(void)
Definition mpgui_gtk5.c:172
static GtkWidget * statusbar
Definition mpgui_gtk5.c:40
static GListStore * main_store
Definition mpgui_gtk5.c:43
static gboolean query_main_list_tooltip_cb(GtkWidget *widget, gint x, gint y, gboolean keyboard_tip, GtkTooltip *tooltip, gpointer data)
Definition mpgui_gtk5.c:411
static void selection_change(GtkSelectionModel *model, guint position, guint n_items, gpointer user_data)
Definition mpgui_gtk5.c:495
static FcMPRow * fc_mprow_new(void)
Definition mpgui_gtk5.c:135
static gboolean versionlist_update_main_thread(gpointer user_data)
Definition mpgui_gtk5.c:286
static void activate_gui(GtkApplication *app, gpointer data)
Definition mpgui_gtk5.c:706
#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:147
#define ML_COL_LIC
Definition mpgui_gtk5.c:63
static void pbar_callback(int downloaded, int max)
Definition mpgui_gtk5.c:212
static void URL_return(GtkEntry *w, gpointer data)
Definition mpgui_gtk5.c:399
static GtkWidget * URL_input
Definition mpgui_gtk5.c:44
static void fc_mprow_class_init(FcMPRowClass *klass)
Definition mpgui_gtk5.c:116
static void fc_mprow_finalize(GObject *gobject)
Definition mpgui_gtk5.c:102
#define FC_TYPE_MPROW
Definition mpgui_gtk5.c:72
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:357
static gboolean msg_main_thread(gpointer user_data)
Definition mpgui_gtk5.c:227
static void fc_mprow_init(FcMPRow *self)
Definition mpgui_gtk5.c:127
static void install_clicked(GtkWidget *w, gpointer data)
Definition mpgui_gtk5.c:387
static GtkAlertDialog * quit_dialog
Definition mpgui_gtk5.c:45
static void factory_bind(GtkSignalListItemFactory *self, GtkListItem *list_item, gpointer user_data)
Definition mpgui_gtk5.c:514
#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:452
static GtkApplication * fcmp_app
Definition mpgui_gtk5.c:54
static void pbar_dl_thread(int current, int max)
Definition mpgui_gtk5.c:273
static void msg_callback(const char *msg)
Definition mpgui_gtk5.c:203
#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:156
static void modinst_setup_widgets(void)
Definition mpgui_gtk5.c:564
static void versionlist_update_dl_thread(void)
Definition mpgui_gtk5.c:325
#define ML_COL_URL
Definition mpgui_gtk5.c:64
static gboolean pbar_main_thread(gpointer user_data)
Definition mpgui_gtk5.c:259
static void factory_setup(GtkSignalListItemFactory *self, GtkListItem *list_item, gpointer user_data)
Definition mpgui_gtk5.c:554
#define ML_COL_VER
Definition mpgui_gtk5.c:59
static gpointer download_thread(gpointer data)
Definition mpgui_gtk5.c:333
struct sprite int int y
Definition sprite_g.h:31
struct sprite int x
Definition sprite_g.h:31
GObjectClass parent_class
Definition mpgui_gtk5.c:94
const char * ver
Definition mpgui_gtk5.c:81
const char * lic
Definition mpgui_gtk5.c:85
int type_int
Definition mpgui_gtk5.c:89
const char * inst
Definition mpgui_gtk5.c:82
GObject parent_instance
Definition mpgui_gtk5.c:78
const char * type
Definition mpgui_gtk5.c:83
const char * subtype
Definition mpgui_gtk5.c:84
char * URL
Definition mpgui_gtk5.c:86
const char * notes
Definition mpgui_gtk5.c:87
const char * name
Definition mpgui_gtk5.c:80
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