Plugins: replace get_editable with get_flags, add support more MovieSource flags
[cinaest] / src / plugins / catalog-plugin.vala
index cd7b33a..8f72985 100644 (file)
@@ -31,16 +31,34 @@ class CatalogPlugin : Plugin {
                // Make sure the data directory is available
                DirUtils.create_with_parents (data_dir, 0770);
 
+               string hidden_sources = null;
+               try {
+                       var config_file = Path.build_filename (Environment.get_user_config_dir (), "cinaest", "cinaest.cfg");
+                       var keyfile = new KeyFile ();
+                       if (keyfile.load_from_file (config_file, KeyFileFlags.NONE)
+                           && keyfile.has_group ("CatalogPlugin")) {
+                               if (keyfile.has_key ("CatalogPlugin", "HiddenSources")) {
+                                       hidden_sources = keyfile.get_string ("CatalogPlugin", "HiddenSources");
+                               }
+                       }
+               } catch (Error e) {
+                       if (!(e is KeyFileError.NOT_FOUND))
+                               stdout.printf ("Error loading configuration: %s\n", e.message);
+               }
+
                sqlite = new CatalogSqlite (filename);
                sources = new List<CatalogSource> ();
 
-               var source = new CatalogSource ("Collection", _("Collection"), _("Personal movie list"), sqlite);
+               var source = new CatalogSource ("Collection", _("Collection"), _("Personal movie list"), sqlite, !("Collection" in hidden_sources));
                sources.append (source);
 
-               source = new CatalogSource ("Loaned", _("Loaned movies"), _("Movies loaned to friends"), sqlite);
+               source = new CatalogSource ("Loaned", _("Loaned movies"), _("Movies loaned to friends"), sqlite, !("Loaned" in hidden_sources));
                sources.append (source);
 
-               source = new CatalogSource ("Watchlist", _("Watchlist"), _("Movies of interest"), sqlite);
+               source = new CatalogSource ("Watched", _("Watched movies"), _("Watched / rated movies"), sqlite, !("Watched" in hidden_sources));
+               sources.append (source);
+
+               source = new CatalogSource ("Watchlist", _("Watchlist"), _("Movies of interest"), sqlite, !("Watchlist" in hidden_sources));
                sources.append (source);
 
                stdout.printf ("Catalog Plugin Loaded.\n");
@@ -72,7 +90,7 @@ class CatalogPlugin : Plugin {
                        }
                }
 
-               var source_list = new SourceListView (available_sources);
+               var source_list = new SourceListView (available_sources, true, window);
 
                var content = (VBox) dialog.get_content_area ();
                content.pack_start (source_list, true, true, 0);
@@ -87,10 +105,62 @@ class CatalogPlugin : Plugin {
                int res = dialog.run ();
                if (res >= 0) {
                        var source = sources.nth_data (res);
-                       source.add_movie (movie);
 
-                       var banner = (Banner) Banner.show_information_with_markup (window, null, _("'%s' added to list '%s'").printf (movie.title, source.get_name ()));
-                       banner.set_timeout (1500);
+                       if (source.table == "Loaned") {
+                               var dialog = new Gtk.Dialog ();
+                               dialog.set_title (_("Add to loaned movies"));
+
+                               var contact = new Hildon.Entry (SizeType.FINGER_HEIGHT);
+                               contact.set_placeholder ("Contact");
+                               var date = new Hildon.DateButton (SizeType.FINGER_HEIGHT, ButtonArrangement.VERTICAL);
+                               date.set_title (_("Loaned on"));
+                               date.set_alignment (0.0f, 0.5f, 1.0f, 1.0f);
+
+                               content = (Gtk.VBox) dialog.get_content_area ();
+                               content.pack_start (contact, true, false, 0);
+                               content.pack_start (date, true, false, 0);
+
+                               dialog.add_button (_("Done"), Gtk.ResponseType.OK);
+                               dialog.show_all ();
+                               res = dialog.run ();
+                               dialog.destroy ();
+                               if (res == Gtk.ResponseType.OK) {
+                                       source.add_movie (movie);
+
+                                       var banner = (Banner) Banner.show_information_with_markup (window, null, _("'%s' added to list of loaned movies").printf (movie.title, source.get_name ()));
+                                       banner.set_timeout (1500);
+                               }
+                       } else if (source.table == "Watched") {
+                               var dialog = new Gtk.Dialog ();
+                               dialog.set_title (_("Add to watched movies"));
+
+                               var rating = new RatingWidget ();
+                               var date = new Hildon.DateButton (SizeType.FINGER_HEIGHT, ButtonArrangement.VERTICAL);
+                               date.set_title (_("Watched on"));
+                               date.set_alignment (0.0f, 0.5f, 1.0f, 1.0f);
+
+                               content = (Gtk.VBox) dialog.get_content_area ();
+                               content.pack_start (rating, true, false, 0);
+                               content.pack_start (date, true, false, 0);
+
+                               dialog.add_button (_("Done"), Gtk.ResponseType.OK);
+                               dialog.show_all ();
+                               res = dialog.run ();
+                               dialog.destroy ();
+                               if (res == Gtk.ResponseType.OK) {
+                                       if (rating.get_rating () > 0)
+                                               movie.rating = 10 * rating.get_rating ();
+                                       source.add_movie (movie);
+
+                                       var banner = (Banner) Banner.show_information_with_markup (window, null, _("'%s' added to list of watched movies").printf (movie.title, source.get_name ()));
+                                       banner.set_timeout (1500);
+                               }
+                       } else {
+                               source.add_movie (movie);
+
+                               var banner = (Banner) Banner.show_information_with_markup (window, null, _("'%s' added to list '%s'").printf (movie.title, source.get_name ()));
+                               banner.set_timeout (1500);
+                       }
                }
                dialog.destroy ();
                dialog = null;
@@ -109,18 +179,116 @@ class CatalogPlugin : Plugin {
 
                var button = new Hildon.Button (SizeType.FINGER_HEIGHT, ButtonArrangement.VERTICAL);
                button.set_title (_("Select active movie lists"));
-               button.set_value (_("Collection, Loaned movies, Watchlist"));
+               button.set_value (active_sources_text ());
+               button.set_style (ButtonStyle.PICKER);
 
                var content = (VBox) dialog.get_content_area ();
                content.pack_start (button, true, true, 0);
 
                dialog.add_button (_("Done"), ResponseType.ACCEPT);
 
+               // Connect signals
+               button.clicked.connect (() => { on_select_active_lists (button, window); });
+
+               dialog.show_all ();
+               int res = dialog.run ();
+               if (res == ResponseType.ACCEPT) {
+               }
+               dialog.destroy ();
+       }
+
+       private void on_select_active_lists (Hildon.Button button, Gtk.Window window) {
+               dialog = new Gtk.Dialog ();
+               dialog.set_transient_for (window);
+               dialog.set_title (_("Select active movie lists"));
+
+               var source_list = new SourceListView (sources, false, window);
+               source_list.set_hildon_ui_mode (UIMode.EDIT);
+
+               var selection = source_list.get_selection ();
+               foreach (CatalogSource s in sources) {
+                       var iter = TreeIter ();
+
+                       if (s.active && source_list.get_iter (s, out iter)) {
+                               selection.select_iter (iter);
+                       }
+               }
+
+               var content = (VBox) dialog.get_content_area ();
+               content.pack_start (source_list, true, true, 0);
+               var i = sources.length ();
+               if (i > 5)
+                       i = 5;
+               content.set_size_request (-1, (int) i*70);
+
+               dialog.add_button (_("Done"), ResponseType.ACCEPT);
+
                dialog.show_all ();
                int res = dialog.run ();
                if (res == ResponseType.ACCEPT) {
+                       foreach (CatalogSource s in sources) {
+                               TreeIter iter;
+
+                               if (source_list.get_iter (s, out iter)) {
+                                       s.active = selection.iter_is_selected (iter);
+                               }
+                       }
+
+                       var config_file = Path.build_filename (Environment.get_user_config_dir (), "cinaest", "cinaest.cfg");
+                       var keyfile = new KeyFile ();
+                       try {
+                               keyfile.load_from_file (config_file, KeyFileFlags.NONE);
+                       } catch (Error e) {
+                               if (!(e is KeyFileError.NOT_FOUND))
+                                       stdout.printf ("Error loading configuration: %s\n", e.message);
+                       }
+                       keyfile.set_string ("CatalogPlugin", "HiddenSources", hidden_sources_list ());
+
+                       try {
+                               var file = File.new_for_path (config_file + ".part");
+                               var stream = file.create (FileCreateFlags.REPLACE_DESTINATION, null);
+                               var data = keyfile.to_data ();
+
+                               stream.write (data, data.length, null);
+                               FileUtils.rename (config_file + ".part", config_file);
+                       } catch (Error e) {
+                               stdout.printf ("Failed to store configuration: %s\n", e.message);
+                       }
+
+                       button.set_value (active_sources_text ());
                }
                dialog.destroy ();
+               dialog = null;
+       }
+
+       private string active_sources_text () {
+               string text = null;
+
+               foreach (CatalogSource s in sources) {
+                       if (s.active) {
+                               if (text == null) {
+                                       text = s.get_name ();
+                               } else {
+                                       text += ", " + s.get_name ();
+                               }
+                       }
+               }
+               return text;
+       }
+
+       private string hidden_sources_list () {
+               string list = "";
+
+               foreach (CatalogSource s in sources) {
+                       if (!s.active) {
+                               if (list == "") {
+                                       list = s.table;
+                               } else {
+                                       list += ", " + s.table;
+                               }
+                       }
+               }
+               return list;
        }
 
        public override unowned string get_name () {
@@ -129,20 +297,24 @@ class CatalogPlugin : Plugin {
 }
 
 class CatalogSource : MovieSource {
-       private string table;
+       internal string table;
        private string name;
        private string description;
        private CatalogSqlite sqlite;
 
-       public CatalogSource (string _table, string _name, string _description, CatalogSqlite _sqlite) {
+       public CatalogSource (string _table, string _name, string _description, CatalogSqlite _sqlite, bool _active) {
+               GLib.Object (active: _active);
                table = _table;
                name = _name;
                description = _description;
                sqlite = _sqlite;
        }
 
-       public override async void get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
-               yield sqlite.query (table, filter, callback, limit, cancellable);
+       public override bool active { get; set construct; }
+
+       public override async int get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
+               int n = yield sqlite.query (table, filter, callback, limit, cancellable);
+               return n;
        }
 
        public override void add_movie (Movie movie) {
@@ -165,13 +337,13 @@ class CatalogSource : MovieSource {
                return description;
        }
 
-       public override bool get_editable () {
-               return true;
+       public override SourceFlags get_flags () {
+               return SourceFlags.EDITABLE;
        }
 }
 
 [ModuleInit]
-public Type register_plugin () {
+public Type register_plugin (TypeModule module) {
        // types are registered automatically
        return typeof (CatalogPlugin);
 }