
/* 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 Gtk;

namespace GnomeEncfsManager {

    public class InformationDialog {
        private static Window window;
        private static Builder builder;
        
        
        public static void show(Stash stash) {
            if (window != null)
                return;

            string info = stash.getInfo();
            if (info == null) {
                return;
            }

            try {
                builder = new Builder();
                builder.add_from_file(get_glade_file("information_dialog.ui"));
                builder.connect_signals(null);
                window = (Window)builder.get_object("information_dialog");
                
                ((Label)builder.get_object("label_information")).set_text(info);
            
                window.present();
                
            } catch (Error e) {
                stderr.printf("Could not load UI: %s\n", e.message);
            }
        }
        
        public static void show_message(string title, string message) {
            if (window != null)
                return;

            try {
                builder = new Builder();
                builder.add_from_file(get_glade_file("information_dialog.ui"));
                builder.connect_signals(null);
                window = (Window)builder.get_object("information_dialog");
 
                window.set_title(title);
                ((Label)builder.get_object("label_information")).set_line_wrap(true);
                ((Label)builder.get_object("label_information")).set_text(message);
            
                window.present();
                window.set_keep_above(true);
                
            } catch (Error e) {
                stderr.printf("Could not load UI: %s\n", e.message);
            }
        }
        
        public static void hide() {
            if (window == null)
                return;
                
            window.destroy();
            window = (Window)null;
        }
    }
    
    public class About {
        private static AboutDialog dialog;
    
        public static void show() {
            if (dialog != null)
                return;
        
            try {
                Builder builder = new Builder();
                builder.add_from_file(get_glade_file("about.ui"));
                builder.connect_signals(null);
                dialog = (Gtk.AboutDialog)builder.get_object("about");
                dialog.set_version(Config.VERSION);
                dialog.set_copyright(Config.COPYRIGHT);
                dialog.set_website(Config.WEBSITE);
                
		    	dialog.response.connect( (dlg, response_id) => {
				    dialog.destroy();
				    dialog = (AboutDialog)null;
		    	});
                
                dialog.show();

            } catch (Error e) {
                stderr.printf("Could not load UI: %s\n", e.message);
            }
        }
    }
    
    void show_help(string topic) {
    	string uri = Path.build_filename( get_data_path(), "doc", "locale", "C", "index.page" );
    	foreach (string i in Intl.get_language_names()) {
    		string tmp = Path.build_filename( get_data_path(), "doc", "locale", i.split("_")[0], "index.page" );
    		if (FileUtils.test( tmp, FileTest.EXISTS )) {
				uri = tmp;
				break;
			}
    	}
    	
    	uri += "#" + topic;

		try {
			Process.spawn_command_line_async( "yelp \"" + uri + "\"");
		}
		catch (Error e) {
			stderr.printf("%s\n", e.message);
		}
    }
    
	void show_help_cb( LinkButton source ) {
    	show_help(source.uri);
    }
    
    
}
