Add genres structure
authorPhilipp Zabel <philipp.zabel@gmail.com>
Fri, 6 Nov 2009 19:05:44 +0000 (20:05 +0100)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Tue, 10 Nov 2009 11:21:15 +0000 (12:21 +0100)
This wraps the genres bitfield and provides utility functions like to_string.

Makefile.am
src/genres.vala [new file with mode: 0644]
src/imdb/imdb-sqlite.vala
src/movie-filter.vala
src/movie.vala
src/plugins/imdb-plugin.vala

index ace81c7..6991d4b 100644 (file)
@@ -25,6 +25,7 @@ desktopentry_DATA = \
 # Sources
 cinaest_SOURCES = \
         src/main.c \
+       src/genres.c \
         src/movie.c \
        src/movie-filter.c \
         src/movie-list-menu.c \
@@ -36,6 +37,7 @@ cinaest_SOURCES = \
 
 cinaest_VALASOURCES = \
         src/main.vala \
+       src/genres.vala \
         src/movie.vala \
        src/movie-filter.vala \
         src/movie-list-menu.vala \
@@ -55,6 +57,7 @@ cinaest_LDADD = ${HILDON_LIBS} ${OSSO_LIBS} ${GMODULE_LIBS}
 
 libimdb_plugin_la_SOURCES = \
         src/plugins/imdb-plugin.c \
+       src/genres.c \
         src/imdb/imdb-sqlite.c \
         src/movie.c \
         src/plugin-interface.c \
@@ -62,6 +65,7 @@ libimdb_plugin_la_SOURCES = \
 
 libimdb_plugin_la_VALASOURCES = \
         src/plugins/imdb-plugin.vala \
+       src/genres.vala \
         src/imdb/imdb-sqlite.vala \
         src/imdb/plaintext-downloader-interface.vala \
         src/movie.vala \
@@ -85,6 +89,7 @@ imdb_plaintext_downloader_SOURCES = \
 
 imdb_plaintext_downloader_VALASOURCES = \
         src/imdb/imdb-plaintext-downloader.vala \
+       src/genres.vala \
         src/imdb/gzip-input-stream.vala \
         src/imdb/imdb-sqlite.vala \
         src/imdb/plaintext-downloader-interface.vala \
diff --git a/src/genres.vala b/src/genres.vala
new file mode 100644 (file)
index 0000000..e5738f1
--- /dev/null
@@ -0,0 +1,128 @@
+/* This file is part of Cinaest.
+ *
+ * Copyright (C) 2009 Philipp Zabel
+ *
+ * Cinaest 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Cinaest 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 Cinaest. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+public enum GenreType {
+       ANIMATION = 0,
+       ADULT,
+       DRAMA,
+       SHORT,
+       ACTION,
+       FANTASY,
+       HORROR,
+       SCIFI,
+       THRILLER,
+       BIOGRAPHY,
+       COMEDY,
+       MUSICAL,
+       ADVENTURE,
+       DOCUMENTARY,
+       CRIME,
+       MUSIC,
+       SPORT,
+       ROMANCE,
+       HISTORY,
+       MYSTERY,
+       WAR,
+       WESTERN,
+       FAMILY,
+       TALKSHOW,
+       REALITYTV,
+       GAMESHOW,
+       NEWS,
+       FILMNOIR
+}
+
+public struct Genres {
+       private static const string[] _genre_string = {
+               "Animation",
+               "Adult",
+               "Drama",
+               "Short",
+               "Action",
+               "Fantasy",
+               "Horror",
+               "Sci-Fi",
+               "Thriller",
+               "Biography",
+               "Comedy",
+               "Musical",
+               "Adventure",
+               "Documentary",
+               "Crime",
+               "Music",
+               "Sport",
+               "Romance",
+               "History",
+               "Mystery",
+               "War",
+               "Western",
+               "Family",
+               "Talk-Show",
+               "Game-Show",
+               "News",
+               "Film-Noir"
+       };
+
+       public int field;
+
+       public string to_string () {
+               string s = null;
+
+               for (int i = 0; i < 28; i++) {
+                       if ((1 << i) in field) {
+                               if (s == null) {
+                                       s = _genre_string[i];
+                               } else {
+                                       s += ", " + _genre_string[i];
+                               }
+                       }
+               }
+
+               return s;
+       }
+
+       public void set_bit (GenreType genre, bool enable) {
+               if (enable)
+                       field |= (1 << genre);
+               else
+                       field &= ~(1 << genre);
+       }
+
+       public bool get_bit (GenreType genre) {
+               return (genre in field);
+       }
+
+       public static unowned string genre_string (GenreType genre) {
+               return _genre_string[genre];
+       }
+
+/*
+       public static int genre_bit (string genre) {
+               int i = 0;
+
+               do {
+                       if (_genres[i] == genre) {
+                               return (1 << i);
+                       }
+               } while (++i < 28);
+
+               return 0;
+       }
+*/
+}
+
index 26e21fe..af9c317 100644 (file)
@@ -22,7 +22,7 @@ class IMDbSqlite : Object {
        Database db;
        List<string> genres;
 
-       public delegate void ReceiveMovieFunction (string title, int year, int rating);
+       public delegate void ReceiveMovieFunction (string title, int year, int rating, int genres);
 
        public IMDbSqlite (string filename) {
                int rc;
@@ -158,7 +158,7 @@ class IMDbSqlite : Object {
        }
 
        public int query (MovieFilter filter, ReceiveMovieFunction receive_movie) {
-               var sql = "SELECT Title, Year, Rating FROM Movies";
+               var sql = "SELECT Title, Year, Rating, Genres FROM Movies";
                var sep = " WHERE ";
                Statement stmt;
                int rc;
@@ -182,8 +182,8 @@ class IMDbSqlite : Object {
                        sql += sep + "Rating >= = %d".printf (filter.rating_min);
                        sep = " AND ";
                }
-               if (filter.genres > 0) {
-                       sql += sep + "Genres&%d = %d".printf (filter.genres, filter.genres);
+               if (filter.genres.field != 0) {
+                       sql += sep + "Genres&%d = %d".printf (filter.genres.field, filter.genres.field);
                }
                sql += " LIMIT %d;".printf (100);
 
@@ -201,7 +201,8 @@ class IMDbSqlite : Object {
                                string title = stmt.column_text (0);
                                int year = stmt.column_int (1);
                                int rating = stmt.column_int (2);
-                               receive_movie (title, year, rating);
+                               int genres = stmt.column_int (3);
+                               receive_movie (title, year, rating, genres);
                        }
                } while (rc == Sqlite.ROW);
 
index 549c041..d80297b 100644 (file)
@@ -21,5 +21,5 @@ public class MovieFilter {
        public int year_min;
        public int year_max;
        public int rating_min;
-       public int genres;
+       public Genres genres;
 }
index cf970cc..8f0c88c 100644 (file)
@@ -25,7 +25,7 @@ public class Movie : Object {
        public string title { get; set; }
        public int year { get; set; }
        public int rating { get; set; }
-       public int genres { get; set; }
+       public Genres genres;
        public Poster poster { get; set; }
 
        construct {
index c314c64..8a35676 100644 (file)
@@ -95,11 +95,12 @@ class IMDBSource : MovieSource {
                sqlite.query (filter, receive_movie);
        }
 
-       private void receive_movie (string title, int year, int rating) {
+       private void receive_movie (string title, int year, int rating, int genres) {
                Movie movie = new Movie ();
                movie.title = title;
                movie.year = year;
                movie.rating = rating;
+               movie.genres.field = genres;
                _get_callback (movie);
        }