
/* 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;
using GLib;
using Secret;

namespace GnomeEncfsManager {
    public class RemoveStash {
        private static Window window;
        private static Builder builder;
        
        private static Stash stash;

        public static void show(Stash s) {
            if ((window != null) || (s == null))
                return;
        
            try {
                builder = new Builder();
                builder.add_from_file(get_glade_file("remove_stash.ui"));
                builder.connect_signals(null);
                window = (Window)builder.get_object("remove_stash");
                
                stash = s;
                
                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)
                return;

            window.destroy();
            stash = (Stash)null;
            window = (Window)null;
        }
        
        public static void update() {
            if ((window == null) || (stash == null))
                return;
                
            ((Gtk.Widget)builder.get_object("vbox_delete")).set_sensitive(stash.isAvailable);
            
            ((Button)builder.get_object("button_ok")).set_sensitive(false);
            
            ((CheckButton)builder.get_object("checkbutton1")).set_sensitive(((RadioButton)builder.get_object("radiobutton2")).get_active());
            
            if (((RadioButton)builder.get_object("radiobutton1")).get_active()) {
                ((Button)builder.get_object("button_ok")).set_sensitive(true);
            }
            else if (((CheckButton)builder.get_object("checkbutton1")).get_active()) {
                ((Button)builder.get_object("button_ok")).set_sensitive(true);
            }
            
        }
        
        public static void checkbutton1_clicked() {
            ((CheckButton)builder.get_object("checkbutton1")).set_active(false);
            update();
        }
        
        public static void ok() {
            if (((RadioButton)builder.get_object("radiobutton1")).get_active()) {
                // Remove from manager
                            
				if (FileUtils.test(stash.mountDir, FileTest.IS_DIR)) {
				    Posix.rmdir(stash.mountDir);
				}

                Secret.password_clear_sync(Secret.get_schema(Secret.SchemaType.COMPAT_NETWORK), null, "server", stash.cryptDir);
                stashes.remove(stash);
                stash.destroy();
                save_stashes();
		        TrayIcon.update();
		        Manager.update();
                hide();
            }
            else if (((CheckButton)builder.get_object("checkbutton1")).get_active()) {
                // Delete from disk
                if (stash.isMounted) {
	                stash.unmount();
				}
				
				if (FileUtils.test(stash.mountDir, FileTest.IS_DIR)) {
				    Posix.rmdir(stash.mountDir);
				}
				
                string[] argv = new string[3];
                argv[0] = "rm";
                argv[1] = "-rf";
                argv[2] = stash.cryptDir;
                string error_msg;
                
                try {
                    Process.spawn_sync( null, // working directory
                                        argv,
                                        null, // environment
                                        SpawnFlags.SEARCH_PATH,
                                        null, // spawnchild setup
                                        null,
                                        out error_msg,
                                        null );
                } catch (GLib.Error e) {
                    GLib.stderr.printf(e.message+"\n");
                }

                if (FileUtils.test(stash.cryptDir, FileTest.EXISTS)) {
                    // Error deleting stash
                    return;
                }
                
                Secret.password_clear_sync(Secret.get_schema(Secret.SchemaType.COMPAT_NETWORK), null, "server", stash.cryptDir);
                stash.destroy();
                
		        TreeIter iter;

		        stashes.clear();
		        bool valid = Stash.get_model().get_iter_first(out iter);
		        if (valid) {
				    do {
				        Stash.get_model().get(iter, 4, out stash, -1);
				        stash.iter = iter;
				        stashes.add(stash);
				    } while (Stash.get_model().iter_next(ref iter));
				}
		        
		        TrayIcon.update();
		        Manager.update();
		        save_stashes();

                hide();
            }
        }
    }
}
