f42a2bca402e6cbff57b0e61790ed0f90836f7a4
[cinaest] / src / plugins / catalog-plugin.vala
1 /* This file is part of Cinaest.
2  *
3  * Copyright (C) 2009 Philipp Zabel
4  *
5  * Cinaest is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Cinaest is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Cinaest. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using Gtk;
20 using Hildon;
21
22 class CatalogPlugin : Plugin {
23         List<CatalogSource> sources;
24         private CatalogSqlite sqlite;
25         private Gtk.Dialog dialog;
26
27         public override void hello (Gtk.Window window, Osso.Context context) {
28                 string data_dir = Path.build_filename (Environment.get_user_data_dir(), "cinaest");
29                 string filename = Path.build_filename (data_dir, "catalog.db");
30
31                 // Make sure the data directory is available
32                 DirUtils.create_with_parents (data_dir, 0770);
33
34                 sqlite = new CatalogSqlite (filename);
35                 sources = new List<CatalogSource> ();
36
37                 var source = new CatalogSource ("Collection", _("Collection"), _("Personal movie list"), sqlite);
38                 sources.append (source);
39
40                 source = new CatalogSource ("Loaned", _("Loaned movies"), _("Movies loaned to friends"), sqlite);
41                 sources.append (source);
42
43                 source = new CatalogSource ("Watchlist", _("Watchlist"), _("Movies of interest"), sqlite);
44                 sources.append (source);
45
46                 stdout.printf ("Catalog Plugin Loaded.\n");
47         }
48
49         public override unowned List<MovieSource> get_sources () {
50                 return (List<MovieSource>) sources;
51         }
52
53         public override List<MovieAction> get_actions (Movie movie, Gtk.Window window) {
54                 var list = new List<MovieAction> ();
55
56                 list.append (new MovieAction (_("Add to catalog"), on_add_to_catalog, movie, window));
57
58                 return list;
59         }
60
61         private void on_add_to_catalog (Movie movie, Gtk.Window window) {
62                 dialog = new Gtk.Dialog ();
63                 dialog.set_transient_for (window);
64                 dialog.set_title (_("Add movie to catalog - Select list"));
65
66                 int i = 0;
67                 var available_sources = new List<MovieSource> ();
68                 foreach (CatalogSource s in sources) {
69                         if (!s.contains (movie)) {
70                                 available_sources.append ((MovieSource) s);
71                                 i++;
72                         }
73                 }
74
75                 var source_list = new SourceListView (available_sources, true);
76
77                 var content = (VBox) dialog.get_content_area ();
78                 content.pack_start (source_list, true, true, 0);
79                 if (i > 5)
80                         i = 5;
81                 content.set_size_request (-1, i*70);
82
83                 // Connect signals
84                 source_list.source_activated.connect (on_source_activated);
85
86                 dialog.show_all ();
87                 int res = dialog.run ();
88                 if (res >= 0) {
89                         var source = sources.nth_data (res);
90                         source.add_movie (movie);
91
92                         var banner = (Banner) Banner.show_information_with_markup (window, null, _("'%s' added to list '%s'").printf (movie.title, source.get_name ()));
93                         banner.set_timeout (1500);
94                 }
95                 dialog.destroy ();
96                 dialog = null;
97         }
98
99         private void on_source_activated (MovieSource source) {
100                 int n = sources.index ((CatalogSource) source);
101
102                 dialog.response (n);
103         }
104
105         public override void settings_dialog (Gtk.Window window) {
106                 var dialog = new Gtk.Dialog ();
107                 dialog.set_transient_for (window);
108                 dialog.set_title (_("Catalog plugin settings"));
109
110                 var button = new Hildon.Button (SizeType.FINGER_HEIGHT, ButtonArrangement.VERTICAL);
111                 button.set_title (_("Select active movie lists"));
112                 button.set_value (_("Collection, Loaned movies, Watchlist"));
113
114                 var content = (VBox) dialog.get_content_area ();
115                 content.pack_start (button, true, true, 0);
116
117                 dialog.add_button (_("Done"), ResponseType.ACCEPT);
118
119                 dialog.show_all ();
120                 int res = dialog.run ();
121                 if (res == ResponseType.ACCEPT) {
122                 }
123                 dialog.destroy ();
124         }
125
126         public override unowned string get_name () {
127                 return _("Catalog");
128         }
129 }
130
131 class CatalogSource : MovieSource {
132         private string table;
133         private string name;
134         private string description;
135         private CatalogSqlite sqlite;
136
137         public CatalogSource (string _table, string _name, string _description, CatalogSqlite _sqlite) {
138                 GLib.Object (active: true);
139                 table = _table;
140                 name = _name;
141                 description = _description;
142                 sqlite = _sqlite;
143         }
144
145         public override bool active { get; set construct; }
146
147         public override async void get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
148                 yield sqlite.query (table, filter, callback, limit, cancellable);
149         }
150
151         public override void add_movie (Movie movie) {
152                 sqlite.add_movie (table, movie);
153         }
154
155         public override void delete_movie (Movie movie) {
156                 sqlite.delete_movie (table, movie);
157         }
158
159         internal bool contains (Movie movie) {
160                 return sqlite.contains (table, movie);
161         }
162
163         public override unowned string get_name () {
164                 return name;
165         }
166
167         public override unowned string get_description () {
168                 return description;
169         }
170
171         public override bool get_editable () {
172                 return true;
173         }
174 }
175
176 [ModuleInit]
177 public Type register_plugin () {
178         // types are registered automatically
179         return typeof (CatalogPlugin);
180 }