melonvpn/src/impl/gui/main.c

135 lines
3.9 KiB
C
Raw Normal View History

2021-04-24 11:19:26 +01:00
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <libappindicator/app-indicator.h>
#include "states.h"
#include "statusicon.h"
#include "magic_code.h"
GtkApplication *app;
GdkPixbuf *appicon;
char *statusiconoff;
char *statusiconon;
States currentstate;
void update_state(States state) {
currentstate = state;
if(currentstate == CONNECTED) {
status_icon_set_state(APP_INDICATOR_STATUS_ATTENTION);
} else if(currentstate == DISCONNECTED) {
status_icon_set_state(APP_INDICATOR_STATUS_PASSIVE);
} else {
status_icon_set_state(APP_INDICATOR_STATUS_ACTIVE);
}
}
void open_about(GtkApplication* app) {
GtkWidget *about = gtk_about_dialog_new();
gtk_about_dialog_set_program_name(GTK_ABOUT_DIALOG(about),"Melon VPN");
gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(about),"v1.0.0");
gtk_about_dialog_set_copyright(GTK_ABOUT_DIALOG(about),"Copyright OnPointCoding");
gtk_about_dialog_set_license_type(GTK_ABOUT_DIALOG(about),GTK_LICENSE_UNKNOWN);
gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(about),"https://software.onpointcoding.net");
gtk_about_dialog_set_website_label(GTK_ABOUT_DIALOG(about),"OnPointCoding.net");
gtk_about_dialog_set_logo(GTK_ABOUT_DIALOG(about),appicon);
const char *authors[2] = {"MrMelon54",NULL};
gtk_about_dialog_set_authors(GTK_ABOUT_DIALOG(about),authors);
const char *artists[2] = {"MrMelon54",NULL};
gtk_about_dialog_set_artists(GTK_ABOUT_DIALOG(about),artists);
gtk_widget_show_all(about);
}
void open_paint_btn(GtkButton *button) {
update_state((currentstate + 1) % 3);
}
void open_about_btn(GtkButton *button) {
open_about(app);
}
void activate(GtkApplication* app) {
GtkWidget *window;
GtkWidget *box;
GtkWidget *button;
GtkWidget *button2;
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window),"Melon VPN");
gtk_window_set_default_size(GTK_WINDOW(window),400,400);
// Setup status icon now window is available
status_icon_create(window,statusiconoff,statusiconon);
status_icon_set_state(APP_INDICATOR_STATUS_PASSIVE);
gtk_container_set_border_width(GTK_CONTAINER(window),8);
button = gtk_button_new_with_label("Start");
g_signal_connect(button,"clicked",G_CALLBACK(open_paint_btn),NULL);
button2 = gtk_button_new_with_label("About");
g_signal_connect(button2,"clicked",G_CALLBACK(open_about_btn),NULL);
box = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window),box);
gtk_fixed_put(GTK_FIXED(box),button,5,5);
gtk_fixed_put(GTK_FIXED(box),button2,100,60);
gtk_widget_show_all(window);
}
void get_magic_code(int *a, int *b) {
*a = MAGIC_CODE_A;
*b = MAGIC_CODE_B;
}
int run_gui(int argc, char **argv) {
int status;
statusiconoff = "";
statusiconon = "";
if(access("melonvpn.png",R_OK) == 0) {
appicon = gdk_pixbuf_new_from_file("melonvpn.png",NULL);
} else if(access("/usr/lib/melonvpn/melonvpn.png",R_OK) == 0) {
appicon = gdk_pixbuf_new_from_file("/usr/lib/melonvpn/melonvpn.png",NULL);
}
if(access("/usr/lib/melonvpn/statusmelonoff.png",R_OK) == 0) {
statusiconoff = "/usr/lib/melonvpn/statusmelonoff.png";
} else {
fprintf(stderr,"WARN: Unable to find statusmelonoff.png\n");
}
if(access("/usr/lib/melonvpn/statusmelonon.png",R_OK) == 0) {
statusiconon = "/usr/lib/melonvpn/statusmelonon.png";
} else {
fprintf(stderr,"WARN: Unable to find statusmelonon.png\n");
}
if(appicon == NULL) {
fprintf(stderr,"ERROR: Failed to load program logo\n");
return 1;
}
if(statusiconoff == NULL) {
fprintf(stderr,"ERROR: Failed to load status off icon\n");
return 1;
}
if(statusiconon == NULL) {
fprintf(stderr,"ERROR: Failed to load status on icon\n");
return 1;
}
currentstate = DISCONNECTED;
app = gtk_application_new("net.onpointcoding.melonvpn",G_APPLICATION_FLAGS_NONE);
g_signal_connect(app,"activate",G_CALLBACK(activate),NULL);
status = g_application_run(G_APPLICATION(app),argc,argv);
g_object_unref(app);
return status;
}