A couple of bugfixes in dolconf.
[doldaconnect.git] / config / util / dolconf.c
index 2a4e8a0..7a8b2fa 100644 (file)
@@ -233,7 +233,7 @@ char *getword(char **p)
        p2 = *p + strlen(*p);
     len = p2 - *p - ((*p2 == '\"')?1:0);
     buf = smalloc(len + 1);
-    memcpy(buf, *p, len);
+    memcpy(buf, *p + ((delim == '\"')?1:0), len);
     buf[len] = 0;
     *p = p2 + ((*p2 == '\"')?1:0);
     for(p2 = buf; *p2; p2++, len--) {
@@ -346,7 +346,7 @@ int readconfig(void)
     return(rv);
 }
 
-void writeconfig(void)
+int writeconfig(void)
 {
     FILE *cf;
     struct cfvar *var;
@@ -355,7 +355,7 @@ void writeconfig(void)
     
     if((cf = fopen(cfname, "w")) == NULL) {
        msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("Could not open the configuration file for writing: %s"), strerror(errno));
-       return;
+       return(-1);
     }
     fputs("# This file was generated by dolconf v" VERSION "\n\n", cf);
     for(var = config; var->name != NULL; var++) {
@@ -394,6 +394,7 @@ void writeconfig(void)
     }
     fclose(cf);
     dirty = 0;
+    return(0);
 }
 
 void fillcfw(void)
@@ -639,7 +640,8 @@ void cb_ast_shareadd_clicked(GtkWidget *widget, gpointer uudata)
 
 void cb_cfw_shareadd_clicked(GtkWidget *widget, gpointer uudata)
 {
-    shareadd();
+    if(shareadd())
+       dirty = 1;
 }
 
 void cb_ast_sharerem_clicked(GtkWidget *widget, gpointer uudata)
@@ -656,8 +658,10 @@ void cb_cfw_sharerem_clicked(GtkWidget *widget, gpointer uudata)
 {
     GtkTreeIter iter;
     
-    if(gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(cfw_sharelist)), NULL, &iter))
+    if(gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(cfw_sharelist)), NULL, &iter)) {
        gtk_list_store_remove(shares, &iter);
+       dirty = 1;
+    }
 }
 
 void cb_ast_checkports(GtkWidget *widget, gpointer uudata)
@@ -701,27 +705,52 @@ void cb_cfw_uinet_toggled(GtkWidget *widget, gpointer uudata)
     gtk_widget_set_sensitive(GTK_WIDGET(cfw_uibox), gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)));
 }
 
-void cb_cfw_hup_activate(GtkWidget *widget, gpointer uudata)
+int hupserver(int disperr)
 {
-    int tag;
+    int tag, ret;
     struct dc_response *resp;
     
     if(dc_connectsync2(dc_srv_local, DC_LATEST) < 0) {
-       msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server"));
-       return;
+       if(disperr)
+           msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server"));
+       return(-1);
     }
     if(dc_login(NULL, 1, dc_convnone, NULL) != DC_LOGIN_ERR_SUCCESS) {
        dc_disconnect();
-       msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server"));
-       return;
+       if(disperr)
+           msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server"));
+       return(-1);
     }
+    ret = 0;
     tag = dc_queuecmd(NULL, NULL, L"hup", NULL);
     if((resp = dc_gettaggedrespsync(tag)) != NULL) {
-       if(resp->code != 200)
-           msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server"));
+       if(resp->code != 200) {
+           if(disperr)
+               msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server"));
+           ret = -1;
+       }
        dc_freeresp(resp);
     }
     dc_disconnect();
+    return(ret);
+}
+
+int saveconfig(void)
+{
+    struct cfvar *cv;
+    
+    cfw2conf();
+    if((cv = cfwvalid()) != NULL) {
+       msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, cv->vld->invmsg, cv->rname);
+       return(-1);
+    }
+    return(writeconfig());
+}
+
+/*
+void cb_cfw_hup_activate(GtkWidget *widget, gpointer uudata)
+{
+    hupserver(1);
 }
 
 void cb_cfw_save_activate(GtkWidget *widget, gpointer uudata)
@@ -746,6 +775,29 @@ void cb_cfw_quit_activate(GtkWidget *widget, gpointer uudata)
     gtk_main_quit();
     state = -1;
 }
+*/
+
+void cb_cfw_ok_clicked(GtkWidget *widget, gpointer uudata)
+{
+    if(saveconfig())
+       return;
+    hupserver(0);
+    gtk_main_quit();
+    state = -1;
+}
+
+void cb_cfw_cancel_clicked(GtkWidget *widget, gpointer uudata)
+{
+    gtk_main_quit();
+    state = -1;
+}
+
+void cb_cfw_apply_clicked(GtkWidget *widget, gpointer uudata)
+{
+    if(saveconfig())
+       return;
+    hupserver(1);
+}
 
 int main(int argc, char **argv)
 {
@@ -760,7 +812,7 @@ int main(int argc, char **argv)
     
     gtk_init(&argc, &argv);
     state = -1;
-    while((c = getopt(argc, argv, "haw")) != -1) {
+    while((c = getopt(argc, argv, "hawV")) != -1) {
        switch(c) {
        case 'a':
            state = 1;
@@ -773,9 +825,13 @@ int main(int argc, char **argv)
            printf("\t-h\tDisplay this help message\n");
            printf("\t-a\tGo directly to the assistant\n");
            printf("\t-w\tGo directly to the standard window\n");
+           printf("\t-V\tDisplay version info and exit\n");
+           exit(0);
+       case 'V':
+           printf("%s", RELEASEINFO);
            exit(0);
        default:
-           fprintf(stderr, "usage: dolconf [-haw]\n");
+           fprintf(stderr, "usage: dolconf [-hawV]\n");
            exit(1);
        }
     }