Logo Search packages:      
Sourcecode: gadmin-openvpn-client version File versions  Download package

show_import_window.c

/* GAdmin-OpenVPN - An easy to use GTK+ frontend for the openvpn server.
 * Copyright (C) 2008 Magnus Loef <magnus-swe@telia.com> 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
*/



#include "../config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include "gettext.h"
#include "widgets.h"
#include "allocate.h"
#include "show_info.h"
#include "commands.h"
#include "file_chooser.h"
#include "show_import_window.h"
#include "make_settings_buttons.h"
#include "make_settings_entries.h"
#include "populate_gadmin_openvpn.h"



void apply_import_clicked(struct w *widgets)
{
    gchar *info, *cmd, *path;
    G_CONST_RETURN gchar *import_file;

    import_file = gtk_entry_get_text(GTK_ENTRY(widgets->import_file_entry));

    /* No valid import directory supplied */
    if( import_file==NULL || strlen(import_file) < 4 )
    {
      info = g_strdup_printf(_("Error: Path length is too short.\n"));
      show_info(info);
      g_free(info);
      return;
    }

    if( ! strstr(import_file, ".tar.gz") )
    {
      info = g_strdup_printf(_("Error: Import package should be a compressed .tar.gz package.\n"));
      show_info(info);
      g_free(info);
      return;
    }

    cmd = g_strdup_printf("mkdir -p %s/client/Imported-Setup", OPENVPN_SYSCONF_DIR);
    if( ! run_command(cmd) )
    {
      info = g_strdup_printf(_("Could not create a new setup directory.\n"));
      show_info(info);
      g_free(info);
      return;
    }
    g_free(cmd);


    cmd = g_strdup_printf("tar -zxf %s -C %s/client/Imported-Setup", import_file, OPENVPN_SYSCONF_DIR);
    if( ! run_command(cmd) )
    {
      info = g_strdup_printf(_("Error: Unpacking client setup package has failed.\n"));
      show_info(info);
      g_free(info);
      return;
    }
    g_free(cmd);


    /* The package from the server export has its setup files in the "client" directory.
       Move the files from that directory one level up so that the client will find them. */
    path = g_strdup_printf("%s/client/Imported-Setup/client", OPENVPN_SYSCONF_DIR);
    if( file_exists(path) && strlen(path) > 20 )
    {
      cmd = g_strdup_printf("mv %s/* %s/client/Imported-Setup", path, OPENVPN_SYSCONF_DIR);
      if( ! run_command(cmd) )
      {
          info = g_strdup_printf(_("Error: Moving client setup package has failed.\n"));
          show_info(info);
          g_free(info);
          g_free(cmd);
          g_free(path);
          return;
      }
      g_free(cmd);
      
      /* Remove the client directory */
      cmd = g_strdup_printf("rm -rf %s", path);
      if( ! run_command(cmd) && strlen(path) > 20 )
      {
          info = g_strdup_printf(_("Error: Removing client setup package.\n"));
          show_info(info);
          g_free(info);
          g_free(cmd);
          g_free(path);
          return;
      }
      g_free(cmd);
    }
    g_free(path);

    /* Populate the entire gui */
    populate_gadmin_openvpn(widgets);

    /* Destroy the import window */
    gtk_widget_destroy(widgets->import_window);
}


void select_file_clicked(struct w *widgets)
{
    gchar *path = NULL;

    path = get_dialog_path_selection("FILE", "/home", "None");
    if( path!=NULL )
    {
        gtk_entry_set_text(GTK_ENTRY(widgets->import_file_entry), path);
        g_free(path);
    }
}


/* Show the import window */
void show_import_window(struct w *widgets)
{
    GtkWidget *frame;
    GtkWidget *table;
    GtkWidget *vbox;
    GtkWidget *dir_select_button;
    GtkWidget *cancel_button, *apply_button;
    GtkTooltips *tooltips;
//    gchar *utf8=NULL;
    gchar *info; //, *text;
    int a=0, b=1;

    tooltips = gtk_tooltips_new();

    widgets->import_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(widgets->import_window), GTK_WIN_POS_CENTER);
//    gtk_widget_set_size_request(widgets->import_window, 480, -1);

    /* Set window information */
    info = g_strdup_printf(_("GAdmin-OpenVPN-Server - Import connection"));
    gtk_window_set_title(GTK_WINDOW(widgets->import_window), info);
    g_free(info);

    vbox = gtk_vbox_new(FALSE, 0);
    gtk_container_add(GTK_CONTAINER(widgets->import_window), vbox);

    tooltips = gtk_tooltips_new();

    /* 1 tables with some settings and 2 columns */
    table = gtk_table_new(2, 2, FALSE);

    frame = gtk_frame_new(_("Select a connection setup package to import:"));

    gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 1);
    gtk_container_add(GTK_CONTAINER(frame), table);


    widgets->import_file_entry = gtk_entry_new();
    dir_select_button = make_button_with_entry(GTK_TABLE(table),
                                  widgets->import_file_entry,
                                  "gtk-refresh",
                                  _("import package:"),
    _("Select a connection setup tar.gz package with a configuration file and certificates to import"),
                                  0,1,a,b,350);
    a++; b++;
    g_signal_connect_swapped(G_OBJECT(dir_select_button), "clicked",
                 G_CALLBACK(select_file_clicked), widgets);

    /* Buttons: "Cancel" and "Apply" */
    GtkWidget *hbutton_box = gtk_hbutton_box_new();
    gtk_button_box_set_layout(GTK_BUTTON_BOX(hbutton_box), GTK_BUTTONBOX_SPREAD);

    cancel_button = gtk_button_new_from_stock(GTK_STOCK_QUIT);
    apply_button = gtk_button_new_from_stock(GTK_STOCK_APPLY);

    gtk_box_pack_start(GTK_BOX(hbutton_box), cancel_button, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(hbutton_box), apply_button, FALSE, FALSE, 0);
    gtk_container_add(GTK_CONTAINER(vbox), hbutton_box);

    /* Window exit signal */
    g_signal_connect(GTK_WINDOW(widgets->import_window), "delete_event",
                     G_CALLBACK(gtk_widget_destroy), NULL);

    /* Cancel button */
    g_signal_connect_swapped((gpointer)cancel_button, "clicked",
            G_CALLBACK(gtk_widget_destroy), GTK_OBJECT(widgets->import_window));

    /* Apply button signals */
    g_signal_connect_swapped((gpointer)apply_button, "clicked",
             G_CALLBACK(apply_import_clicked), widgets);


    gtk_widget_show_all(widgets->import_window);
}

Generated by  Doxygen 1.6.0   Back to index