
/* Gnome Encfs Manager (GEncfsM)
 * Copyright (C) 2011 Moritz Molch <mail@moritzmolch.de>
 
 * 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 2 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.
*/

using Encfs;
using Gtk;
using Gee;
using GnomeEncfsManager;

namespace GnomeEncfsManager {
    public class ChangePassword {
        private static Window window;
        private static Builder builder;
        private static ulong sighandler;
        
        private static Stash stash;
        private static bool progress = false;
        private static int atomic = 1;
        private static int busy_id;

        public static void show(Stash s) {
            if ((window != null) || (s == null))
                return;
        
            try {
                builder = new Builder();
                builder.add_from_file(get_glade_file("change_password.ui"));
                builder.connect_signals(null);
                window = (Window)builder.get_object("change_password");
                
                stash = s;
                sighandler = stash.changePasswordResponse.connect( (success, msg) => {
                	AtomicInt.inc( ref atomic );
                	progress = false;
		            if (success) {
				        hide();
				        busy(busy_id.to_string());
				    }
				    else {
   						((Widget)builder.get_object("button_ok")).set_sensitive(true);
						((ToggleButton)builder.get_object("button_ok")).set_active(false);
						((Widget)builder.get_object("spinner")).set_visible(false);
						//((Spinner)builder.get_object("spinner")).stop();
				    	Label passwdsMatch = (Label)builder.get_object("label_status");
				        passwdsMatch.set_markup("<span color='red'>✘</span> "+msg);
				        ((Widget)builder.get_object("change_password")).set_sensitive(true);
						((Widget)builder.get_object("entry_old_password")).set_sensitive(true);
						((Widget)builder.get_object("entry_old_password")).grab_focus();
						((Widget)builder.get_object("entry_new_password")).set_sensitive(true);
						((Widget)builder.get_object("entry_retype_password")).set_sensitive(true);
						((Widget)builder.get_object("button_cancel")).set_sensitive(true);
				        busy(busy_id.to_string());
				    }
                });
                update();
                window.show_all();

            } catch (GLib.Error e) {
                stderr.printf("Could not load UI: %s\n", e.message);
            }
        }
        
        public static void hide() {
            if (window == null || progress)
                return;

            window.destroy();
            stash.disconnect(sighandler);
            stash = (Stash)null;
            window = (Window)null;
        }
        
        public static void update() {
            if ((window == null) || (stash == null))
                return;
                
            ((Button)builder.get_object("button_ok")).set_sensitive(true);
            
            if (((Entry)builder.get_object("entry_old_password")).get_text() == "") {
                ((Button)builder.get_object("button_ok")).set_sensitive(false);
            }
            
            /** passwords have to match */
            Entry passwdFld = (Entry)builder.get_object("entry_new_password");
            Entry passwdRtpFld = (Entry)builder.get_object("entry_retype_password");
            Label passwdsMatch = (Label)builder.get_object("label_status");
            
            if ((passwdFld.get_text() == "") && (passwdRtpFld.get_text() == "")) {
                ((Button)builder.get_object("button_ok")).set_sensitive(false);
                passwdsMatch.set_markup("");
            }
            else if (passwdFld.get_text() == passwdRtpFld.get_text()) {
                passwdsMatch.set_markup("<span color='green'>✔</span> "+_("Passwords match"));
            }
            else {
                ((Widget)builder.get_object("button_ok")).set_sensitive(false);
                passwdsMatch.set_markup("<span color='red'>✘</span> "+_("Passwords do not match"));
            }
        }
        
        public static void cancel() {
        	hide();
        }
        
        public static void ok_toggled( ToggleButton button ) {
        	if (button.get_active()) {
        		ok();
        	}
        }
        
        public static void ok() {
            if ((window == null) || (stash == null) || progress) {
                return;
            }
            
            if (!AtomicInt.dec_and_test( ref atomic )) {
            	AtomicInt.inc( ref atomic );
            	return;
            }
            /** passwords have to match */
            Entry passwdFld = (Entry)builder.get_object("entry_new_password");
            Entry passwdRtpFld = (Entry)builder.get_object("entry_retype_password");
            Entry passwdFldOld = (Entry)builder.get_object("entry_old_password");
            
            if ((passwdFld.get_text() == "") && (passwdRtpFld.get_text() == "")) {
                return;
            }
            else if (passwdFld.get_text() == passwdRtpFld.get_text()) {
                
            }
            else {
                return;
            }
            
            progress = true;
            busy_id = busy(Path.get_basename(stash.mountDir) + ": " + _("Changing password..."));
            ((ToggleButton)builder.get_object("button_ok")).set_active(true);
            ((Widget)builder.get_object("entry_old_password")).set_sensitive(false);
            ((Widget)builder.get_object("entry_new_password")).set_sensitive(false);
            ((Widget)builder.get_object("entry_retype_password")).set_sensitive(false);
            ((Spinner)builder.get_object("spinner")).start();
            ((Widget)builder.get_object("button_cancel")).set_sensitive(false);
            ((Widget)builder.get_object("button_ok")).set_sensitive(false);
            ((Widget)builder.get_object("spinner")).set_visible(true);
            ((Label)builder.get_object("label_status")).set_markup("");
            //((Widget)builder.get_object("button_cancel")).grab_focus();


            while (Gtk.events_pending()) {
                Gtk.main_iteration();
            }
            
            stash.changePassword(   passwdFldOld.get_text(),
                                    passwdFld.get_text());
        }
    }
}
