// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
/*-
 * Copyright (c) 2013-2014 Birdie Developers (http://birdieapp.github.io)
 *
 * This software is licensed under the GNU General Public License
 * (version 3 or later). See the COPYING file in this distribution.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this software; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Authored by: Ivo Nunes <ivoavnunes@gmail.com>
 *              Vasco Nunes <vascomfnunes@gmail.com>
 */

namespace Birdie {

    [DBus (name = "org.birdieapp.birdie")]
    public class Server : Object {

        Birdie birdie;
        //this.birdie = birdie;

        public void show_tweet_dialog() throws Error {
            Widgets.TweetDialog dialog = new Widgets.TweetDialog (birdie);
            dialog.show_all ();
        }
        
        public void switch_timeline (string timeline) throws Error {
            stdout.printf ("Switching timeline to %s\n", timeline);
            birdie.switch_timeline (timeline);
            birdie.home.set_active (true);
            birdie.activate ();
        }
        
        public void ping_error () throws Error {
            throw new BirdieError.SOME_ERROR ("There was an error!");
        }        

        public signal void pong (int count, string msg);
    }

    [DBus (name = "org.birdieapp.birdie")]
    public errordomain BirdieError {
        SOME_ERROR
    }
    
    
    void on_bus_aquired (DBusConnection conn) {
        try {
            conn.register_object ("/org/birdieapp/birdie", new Server());
        } catch (IOError e) {
            stderr.printf ("Could not register service\n");
        }
    }

    namespace Option {
        private static bool DEBUG = false;
        private static bool START_HIDDEN = false;
    }

    public static int main (string[] args) {

        var context = new OptionContext ("Birdie");
        context.add_main_entries (Birdie.app_options, "birdie");
        context.add_group (Gtk.get_option_group(true));

        try {
            context.parse (ref args);
        } catch (Error e) {
            warning (e.message);
        }

        Gtk.init (ref args);
        var app = new Birdie ();

        var settings = new Settings ("org.birdieapp.birdie");

        var keybinding_manager = new Utils.KeybindingManager ();
        keybinding_manager.init ();
        keybinding_manager.bind (settings.get_string ("tweet-shortcut"), app.new_tweet_keybinding);
        
        Bus.own_name (BusType.SESSION, "org.birdieapp.birdie", BusNameOwnerFlags.NONE,
              on_bus_aquired,
              () => {},
              () => stderr.printf ("Could not aquire name\n"));

        return app.run (args);
    }
}
