summaryrefslogtreecommitdiffstats
path: root/gncpmount.c
blob: 2a61daed247a8d98ce32669484a4f8fb77fbe5b2 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <stdlib.h>
#include <gtk/gtk.h>

#include "dialogs.h"
#include "globals.h"

#define WIDTH 400
#define HEIGHT -1




/**
* Show info, error or warning message
*/
void
show_message (GtkMessageType type, const gchar *format, gchar *msgtxt, const gchar *format_sec, gchar *msgtxt_sec)
{
    GtkWidget *dialog;

    dialog = gtk_message_dialog_new ( GTK_WINDOW (gui.win),
                                         GTK_DIALOG_DESTROY_WITH_PARENT,
                                         type,
                                         GTK_BUTTONS_OK,
                                         format,
                                         msgtxt);

    gtk_message_dialog_format_secondary_text ( GTK_MESSAGE_DIALOG (dialog),
                                              format_sec,
                                              msgtxt_sec);
    gtk_dialog_run ( GTK_DIALOG  (dialog));
    gtk_widget_destroy (dialog);
}


/**
* Create Open folder dialog and let user select folder
* for mount point
*/
static void
open_folder (GtkWidget *wid, GtkWidget *win)
{
    GtkWidget *dialog;
    dialog = gtk_file_chooser_dialog_new ("Select folder - mount point",
                                          GTK_WINDOW (gui.win),
                                          GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                          GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                          NULL);
    if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
    {
        char *folder;
        folder = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
        gtk_entry_set_text (GTK_ENTRY (gui.entry_mount_point), folder);
        g_free (folder);
    }
    gtk_widget_destroy (dialog);

}



static void
passwd_usage (GtkWidget *wid, GtkWidget *win)
{
    cmd_params.n = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (gui.check_n));

    gtk_entry_set_editable (GTK_ENTRY(gui.entry_password), !cmd_params.n);

    if(cmd_params.n)
    {
        gtk_entry_set_visibility (GTK_ENTRY(gui.entry_password), TRUE);
        set_parameter_text (gui.entry_password, &(cmd_params.password));
        gtk_entry_set_text (GTK_ENTRY(gui.entry_password), "no password will be used");
    }
    else
    {
        gtk_entry_set_visibility (GTK_ENTRY(gui.entry_password), FALSE);
        if(cmd_params.password != NULL)
            gtk_entry_set_text (GTK_ENTRY(gui.entry_password), cmd_params.password);
    }
}



static void
run_options_dialog ( GtkWidget *w, gpointer   data )
{
    options_dialog(&gui);
}


static void
set_gui_from_cmdparams ()
{

    if(cmd_params.username != NULL)
        gtk_entry_set_text(GTK_ENTRY(gui.entry_username), cmd_params.username);

    if(cmd_params.password != NULL)
        gtk_entry_set_text(GTK_ENTRY(gui.entry_password), cmd_params.password);

    if(cmd_params.server != NULL)
        gtk_entry_set_text(GTK_ENTRY(gui.entry_server), cmd_params.server);

    if(cmd_params.dns_name != NULL)
        gtk_entry_set_text(GTK_ENTRY(gui.entry_dns_server), cmd_params.dns_name);

    if(cmd_params.mount_point != NULL)
        gtk_entry_set_text(GTK_ENTRY(gui.entry_mount_point), cmd_params.mount_point);

    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (gui.check_n), cmd_params.n);
}




/* Menu, an array of GtkItemFactoryEntry structures that defines each menu item */
static GtkItemFactoryEntry menu_items[] = {
  { "/_File",         NULL,         NULL,           0, "<Branch>" },
  { "/File/_New",     "<control>N", NULL,    0, "<StockItem>", GTK_STOCK_NEW },
  { "/File/_Load",    "<control>L", NULL,    0, "<StockItem>", GTK_STOCK_OPEN },
  { "/File/_Save",    "<control>S", NULL,    0, "<StockItem>", GTK_STOCK_SAVE },
  { "/File/sep1",     NULL,         NULL,           0, "<Separator>" },
  { "/File/_Quit",    "<CTRL>Q", gtk_main_quit, 0, "<StockItem>", GTK_STOCK_QUIT },
  { "/Tools",      NULL,         NULL,           0, "<Branch>" },
  { "/_Tools/Options",  "<control>O",   run_options_dialog,            0, "<Item>" },
  { "/_Help",         NULL,         NULL,           0, "<Branch>" },
  { "/_Help/About",   NULL,         NULL,           0, "<Item>" },
};


static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);


/* Returns a menubar widget made from the above menu */
static GtkWidget *
get_menubar_menu( GtkWidget  *window )
{
  GtkItemFactory *item_factory;
  GtkAccelGroup *accel_group;

  /* Make an accelerator group (shortcut keys) */
  accel_group = gtk_accel_group_new ();

  /* Make an ItemFactory (that makes a menubar) */
  item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>",
                                       accel_group);

  /* This function generates the menu items. Pass the item factory,
     the number of items in the array, the array itself, and any
     callback data for the the menu items. */
  gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, NULL);

  /* Attach the new accelerator group to the window. */
  gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);

  /* Finally, return the actual menu bar created by the item factory. */
  return gtk_item_factory_get_widget (item_factory, "<main>");
}



int main (int argc, char *argv[])
{
    GtkWidget *label = NULL;
    GtkWidget *tmp = NULL;
    GtkWidget *hbox = NULL;

    GtkWidget *halign = NULL;

    GtkWidget *logo = NULL;

    /* Initialize GTK+ */
    g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
    gtk_init (&argc, &argv);
    g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);

    /* Create the main window */
    gui.win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (gui.win), GUI_TITLE );
    gtk_window_set_position (GTK_WINDOW (gui.win), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size (GTK_WINDOW (gui.win), WIDTH, HEIGHT);
    gtk_window_set_resizable (GTK_WINDOW (gui.win), FALSE);

    gtk_widget_realize (gui.win);
    g_signal_connect (gui.win, "destroy", gtk_main_quit, NULL);


    tmp = gtk_vbox_new (FALSE, 1);
    gtk_container_add (GTK_CONTAINER (gui.win), tmp);

    gui.menu = get_menubar_menu (gui.win);
    gtk_box_pack_start (GTK_BOX (tmp), gui.menu, FALSE, TRUE, 0);

    /* Create a vertical box */
    gui.vbox = gtk_vbox_new (FALSE, 6);
    gtk_box_pack_start (GTK_BOX (tmp), gui.vbox,  FALSE, TRUE, 0);

    gtk_container_set_border_width (GTK_CONTAINER (gui.vbox), 10);

    logo = gtk_image_new_from_file("gncpmount.png");
    gtk_box_pack_start (GTK_BOX (gui.vbox), logo, TRUE, TRUE, 0);

    /* Create a notebook */
    gui.notebook = gtk_notebook_new ();
    gtk_notebook_set_tab_pos (GTK_NOTEBOOK (gui.notebook), GTK_POS_TOP);
    gtk_box_pack_start (GTK_BOX (gui.vbox), gui.notebook, TRUE, TRUE, 0);
    gtk_widget_set_size_request(gui.notebook, WIDTH,-1);


    /* Page Login */

    /* Create table */
    gui.table = gtk_table_new (3, 2, FALSE);
    label = gtk_label_new ("Login");
    gtk_notebook_prepend_page (GTK_NOTEBOOK (gui.notebook), gui.table, label);

    /* Labels */
    halign = gtk_alignment_new(0, 0, 0, 1);

    label = gtk_label_new ("Username [-U]:");
    gtk_container_add(GTK_CONTAINER(halign), label);
    gtk_table_attach(GTK_TABLE(gui.table), halign, 0, 1, 0, 1,  GTK_FILL , GTK_FILL | GTK_EXPAND, 4, 0);

    halign = gtk_alignment_new(0, 0, 0, 1);
    label = gtk_label_new ("Password [-P]:");
    gtk_container_add(GTK_CONTAINER(halign), label);
    gtk_table_attach(GTK_TABLE(gui.table), halign, 0, 1, 1, 2,  GTK_FILL, GTK_FILL | GTK_EXPAND, 4, 0);

    /* Chcekbox */
    gui.check_n = gtk_check_button_new_with_label ("Do not use any password [-n]");
    g_signal_connect (G_OBJECT (gui.check_n), "toggled", G_CALLBACK (passwd_usage), NULL);
    gtk_table_attach_defaults (GTK_TABLE (gui.table), gui.check_n, 1, 2, 2, 3);


    /* Entry */
    gui.entry_username = gtk_entry_new ();
    gtk_table_attach_defaults (GTK_TABLE (gui.table),  gui.entry_username, 1, 2, 0, 1);

    gui.entry_password = gtk_entry_new ();
    gtk_entry_set_visibility (GTK_ENTRY(gui.entry_password), FALSE);
    gtk_table_attach_defaults (GTK_TABLE (gui.table),  gui.entry_password, 1, 2, 1, 2);



    /* Page SERVER */
    gui.table = gtk_table_new (4, 2, FALSE);

    label = gtk_label_new ("Server");
    gtk_notebook_append_page (GTK_NOTEBOOK (gui.notebook), gui.table, label);

    halign = gtk_alignment_new(0, 0, 0, 1);
    label = gtk_label_new ("Mount point:");
    gtk_container_add(GTK_CONTAINER(halign), label);
    gtk_table_attach(GTK_TABLE(gui.table), halign, 0, 1, 0, 1,  GTK_FILL , GTK_FILL | GTK_EXPAND, 6, 0);

    hbox = gtk_hbox_new (FALSE, 2);
    gtk_table_attach_defaults (GTK_TABLE (gui.table), hbox, 1, 2, 0, 1);
    gui.entry_mount_point = gtk_entry_new ();
    gtk_container_add(GTK_CONTAINER(hbox), gui.entry_mount_point);

    gui.btn_browse = gtk_button_new_with_label  ("Browse");
    g_signal_connect (gui.btn_browse, "clicked", G_CALLBACK (open_folder), NULL);
    gtk_container_add(GTK_CONTAINER(hbox), gui.btn_browse);

    tmp = gtk_hseparator_new();
    gtk_table_attach (GTK_TABLE(gui.table), tmp, 0, 2, 1, 2,  GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 6, 6);


    halign = gtk_alignment_new(0, 0, 0, 1);
    label = gtk_label_new ("Server [-S]:");
    gtk_container_add(GTK_CONTAINER(halign), label);
    gtk_table_attach(GTK_TABLE(gui.table), halign, 0, 1, 2, 3,  GTK_FILL, GTK_FILL | GTK_EXPAND, 6, 0);

    gui.entry_server = gtk_entry_new ();
    gtk_table_attach_defaults (GTK_TABLE (gui.table),  gui.entry_server, 1, 2, 2, 3);


    halign = gtk_alignment_new(0, 0, 0, 1);
    label = gtk_label_new ("DNS name [-A]:");
    gtk_container_add(GTK_CONTAINER(halign), label);
    gtk_table_attach(GTK_TABLE(gui.table), halign, 0, 1, 3, 4,  GTK_FILL, GTK_FILL | GTK_EXPAND, 6, 0);

    gui.entry_dns_server = gtk_entry_new ();
    gtk_table_attach_defaults (GTK_TABLE (gui.table),  gui.entry_dns_server, 1, 2, 3, 4);



    /* Button box */
    gui.btnbox = gtk_hbutton_box_new();
    gtk_hbutton_box_set_layout_default(GTK_BUTTONBOX_END);
    gtk_box_pack_start (GTK_BOX (gui.vbox), gui.btnbox, TRUE, TRUE, 0);

    /* Buttons */
    gui.btn_mount = gtk_button_new_with_label  ("Mount");
    g_signal_connect (gui.btn_mount, "clicked", gtk_main_quit, NULL);
    gtk_box_pack_start (GTK_BOX (gui.btnbox), gui.btn_mount, TRUE, TRUE, 0);

    gui.btn_close = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
    g_signal_connect (gui.btn_close, "clicked", gtk_main_quit, NULL);
    gtk_box_pack_start (GTK_BOX (gui.btnbox), gui.btn_close, TRUE, TRUE, 0);


    /* Fill in informations from cmd_params to wiidgets */
    set_gui_from_cmdparams();

    /* Enter the main loop */
    gtk_widget_show_all (gui.win);
    gtk_main ();
    return 0;
}