1 /* This file is part of Cinaest.
3 * Copyright (C) 2009 Philipp Zabel
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.
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.
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/>.
22 class GooglePlugin : Plugin {
23 List<MovieSource> sources;
24 List<string> locations;
27 public override void hello (Gtk.Window window, Osso.Context context) {
28 stdout.printf ("Google Plugin Loaded.\n");
30 var source = new GoogleSource ();
32 sources = new List<MovieSource> ();
33 sources.append (source);
37 var config_file = Path.build_filename (Environment.get_user_config_dir (), "cinaest", "cinaest.cfg");
38 var keyfile = new KeyFile ();
39 if (keyfile.load_from_file (config_file, KeyFileFlags.NONE)
40 && keyfile.has_group ("GooglePlugin")) {
41 if (keyfile.has_key ("GooglePlugin", "KnownLocations")) {
42 var l = keyfile.get_string_list ("GooglePlugin", "KnownLocations");
43 for (int i = 0; i < l.length; i++)
44 locations.append (l[i]);
46 if (keyfile.has_key ("GooglePlugin", "LastLocation")) {
47 source.location = last_location = keyfile.get_string ("GooglePlugin", "LastLocation");
51 if (!(e is KeyFileError.NOT_FOUND))
52 stdout.printf ("Error loading configuration: %s\n", e.message);
55 // FIXME - this forces the inclusion of config.h
56 (void) Config.GETTEXT_PACKAGE;
59 public override unowned List<MovieSource> get_sources () {
63 public override List<MovieAction> get_actions (Movie movie, Gtk.Window window) {
64 List<MovieAction> list = null;
69 public override void settings_dialog (Gtk.Window window) {
70 GoogleSource source = (GoogleSource) sources.data;
71 var dialog = new Gtk.Dialog ();
72 dialog.set_transient_for (window);
73 dialog.set_title (_("Google plugin settings"));
75 var selector = new TouchSelectorEntry.text ();
76 insert_location_sorted (source.location);
77 foreach (string l in locations)
78 selector.append_text (l);
80 var button = new PickerButton (SizeType.FINGER_HEIGHT, ButtonArrangement.HORIZONTAL);
81 button.set_title (_("Location"));
82 button.set_selector (selector);
83 button.set_value (source.location);
85 var content = (VBox) dialog.get_content_area ();
86 content.pack_start (button, true, true, 0);
88 dialog.add_button ("Done", ResponseType.ACCEPT);
91 int res = dialog.run ();
92 if (res == ResponseType.ACCEPT) {
93 source.location = button.get_value ();
94 if (insert_location_sorted (source.location) || source.location != last_location) {
95 var config_dir = Path.build_filename (Environment.get_user_config_dir (), "cinaest");
96 var config_file = Path.build_filename (config_dir, "cinaest.cfg");
98 // Make sure the directory is available
99 DirUtils.create_with_parents (config_dir, 0770);
101 var keyfile = new KeyFile ();
103 keyfile.load_from_file (config_file, KeyFileFlags.NONE);
105 if (!(e is KeyFileError.NOT_FOUND))
106 stdout.printf ("Error loading configuration: %s\n", e.message);
108 var l = new string[locations.length ()];
109 for (int i = 0; i < l.length; i++) {
110 l[i] = locations.nth_data (i);
112 keyfile.set_string_list ("GooglePlugin", "KnownLocations", l);
113 keyfile.set_string ("GooglePlugin", "LastLocation", source.location);
114 last_location = source.location;
117 var file = File.new_for_path (config_file + ".part");
118 var stream = file.create (FileCreateFlags.REPLACE_DESTINATION, null);
119 var data = keyfile.to_data ();
121 stream.write (data, data.length, null);
122 FileUtils.rename (config_file + ".part", config_file);
124 stdout.printf ("Failed to store configuration: %s\n", e.message);
131 private bool insert_location_sorted (string? location) {
132 if (location == null)
134 if (locations != null) {
135 for (unowned List<string> l = locations.first (); l != null; l = l.next) {
136 if (l.data == location) {
139 if (l.data > location) {
140 l.insert (location, 0);
145 locations.append (location);
149 public override unowned string get_name () {
154 class GoogleSource : MovieSource {
155 public string location;
156 public string description;
158 public override async void get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
159 var parser = new GoogleParser ();
161 yield parser.query (filter, location, callback, cancellable);
162 if (location == null) {
163 location = parser.location;
167 public override void add_movie (Movie movie) {
170 public override void delete_movie (Movie movie) {
173 public override unowned string get_name () {
177 public override unowned string get_description () {
178 if (location != null && location != "") {
179 description = _("Movie Showtimes near %s").printf (location);
181 description = _("Movie Showtimes");
186 public override bool get_editable () {
192 public Type register_plugin () {
193 // types are registered automatically
194 return typeof (GooglePlugin);