Add AntMovieCatalog importer and exporter, using libxml-2.0
authorPhilipp Zabel <philipp.zabel@gmail.com>
Fri, 6 Aug 2010 16:48:09 +0000 (18:48 +0200)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Fri, 6 Aug 2010 16:38:59 +0000 (18:38 +0200)
Makefile.am
src/import-export-amc.vala [new file with mode: 0644]

index 10f149f..9de75d3 100644 (file)
@@ -49,6 +49,7 @@ pkgconfig_DATA = cinaest.pc
 cinaest_SOURCES = \
         src/main.c \
        src/genre-filter-dialog.c \
+       src/import-export-amc.c \
        src/movie-edit-dialog.c \
         src/movie-list-menu.c \
         src/movie-list-store.c \
@@ -67,6 +68,7 @@ cinaest_SOURCES = \
 cinaest_VALASOURCES = \
         src/main.vala \
        src/genre-filter-dialog.vala \
+       src/import-export-amc.vala \
        src/movie-edit-dialog.vala \
         src/movie-list-menu.vala \
         src/movie-list-store.vala \
@@ -87,13 +89,14 @@ src/main.c: ${cinaest_VALASOURCES}
 
 cinaest_VALAFLAGS = --disable-dbus-transformation --vapidir ./vapi --pkg config --pkg cinaest \
        --pkg dbus-glib-1 --pkg gconf-2.0 --pkg hildon-1 --pkg hildon-fm-2 \
-       --pkg libosso --pkg gmodule-2.0 --pkg gobject-2.0-fix
+       --pkg libosso --pkg gmodule-2.0 --pkg gobject-2.0-fix \
+       --pkg libxml-2.0
 cinaest_CFLAGS = ${CINAEST_CFLAGS} ${DBUS_CFLAGS} ${GCONF_CFLAGS} \
-       ${HILDON_CFLAGS} ${HILDONFM_CFLAGS} \
+       ${HILDON_CFLAGS} ${HILDONFM_CFLAGS} ${XML_CFLAGS} \
        ${MAEMO_LAUNCHER_CFLAGS} ${OSSO_CFLAGS} ${GMODULE_CFLAGS} \
        -DGETTEXT_PACKAGE=\"@GETTEXT_PACKAGE@\"
 cinaest_LDADD = ${CINAEST_LIBS} ${DBUS_LIBS} ${GCONF_LIBS} ${HILDON_LIBS} \
-       ${HILDONFM_LIBS} \
+       ${HILDONFM_LIBS} ${XML_LIBS} \
        ${MAEMO_LAUNCHER_LIBS} ${OSSO_LIBS} ${GMODULE_LIBS} -lcinaest
 
 libcinaest_la_SOURCES = \
diff --git a/src/import-export-amc.vala b/src/import-export-amc.vala
new file mode 100644 (file)
index 0000000..7f9be3d
--- /dev/null
@@ -0,0 +1,153 @@
+class AMCImporter : Object {
+       public bool parse_file (string filename) {
+               Xml.Doc* doc;
+               bool parsed = false;
+
+               doc = Xml.Parser.parse_file (filename);
+               if (doc != null)
+                       parsed = parse_xml (doc);
+
+               delete doc;
+               return parsed;
+       }
+
+       public bool parse_xml (Xml.Doc *doc) {
+               Xml.Node* root;
+
+               root = doc->get_root_element ();
+               if (root == null)
+                       return false;
+               if (root->name != "AntMovieCatalog")
+                       return false;
+
+               for (Xml.Node* catalog = root->children;
+                    catalog != null; catalog = catalog->next) {
+                       if (catalog->name == "text")
+                               continue;
+
+                       if (catalog->name != "Catalog") {
+                               warning ("Unknown node '%s'\n", catalog->name);
+                               continue;
+                       }
+
+                       parse_catalog (catalog);
+               }
+
+               return true;
+       }
+
+       private void parse_catalog (Xml.Node* catalog) {
+               for (Xml.Node* node = catalog->children;
+                    node != null; node = node->next) {
+                       if (node->name == "text")
+                               continue;
+
+                       if (node->name == "Properties")
+                               continue;
+
+                       if (node->name != "Contents") {
+                               warning ("Unknown node '%s'\n", node->name);
+                               continue;
+                       }
+
+                       for (Xml.Node* movie = node->children;
+                            movie != null; movie = movie->next) {
+                               if (movie->name == "text")
+                                       continue;
+
+                               if (movie->name != "Movie") {
+                                       warning ("Unknown node '%s'\n", movie->name);
+                                       continue;
+                               }
+
+                               parse_movie (movie);
+                       }
+               }
+       }
+
+       private void parse_movie (Xml.Node *movie) {
+               var m = new Movie ();
+
+       //      Actors
+       //      AudioFormat
+print ("GENRES: %s\n", movie->get_prop ("Category"));
+               parse_genres (movie->get_prop ("Category"), out m.genres);
+       //      Checked
+       //      Comments
+       //      Country
+       //      Description
+       //      Director
+       //      Disks
+       //      FormattedTitle
+       //      Languages
+       //      m.runtime = movie->get_prop ("Length") * 60;
+       //      Number
+               m.title = movie->get_prop ("OriginalTitle");
+       //      Picture
+               m.rating = movie->get_prop ("Rating").to_int () * 10;
+       //      Subtitles
+       //      TranslatedTitle
+       //      VideoFormat
+               m.year = movie->get_prop ("Year").to_int ();
+
+               movie_available (m);
+       }
+
+       private void parse_genres (string genres_string, out Genres genres) {
+               string[] genre = genres_string.replace (",", " ").replace("|", " ").split (" ");
+               for (int i = 0; i < genre.length; i++)
+                       genres.field |= Genres.genre_bit (genre[i]);
+       }
+
+       public signal void movie_available (Movie movie);
+}
+
+class AMCExporter : Object {
+       public bool write_file (string filename, List<Movie> movies) {
+               Xml.TextWriter writer;
+
+               writer = new Xml.TextWriter.filename (filename);
+               if (writer == null)
+                       return false;
+
+               writer.start_document ("1.0", "iso-8859-1");
+               writer.write_comment ("Cinaest Export in Ant Movie Catalog database format");
+               writer.start_element ("AntMovieCatalog");
+               writer.write_attribute ("Date", "now");
+               writer.write_attribute ("Format", "35");
+               writer.write_attribute ("Version", "3.5.1 (2007-09-22)");
+                       writer.start_element ("Catalog");
+                               writer.write_element ("Properties", "");
+                               writer.start_element ("Contents");
+                               foreach (unowned Movie movie in movies) {
+                                       writer.start_element ("Movie");
+                                       writer.write_attribute ("Actors", "");
+                                       writer.write_attribute ("AudioFormat", "");
+                                       writer.write_attribute ("Category", movie.genres.to_string ());
+                                       writer.write_attribute ("Checked", "");
+                                       writer.write_attribute ("Comments", "");
+                                       writer.write_attribute ("Country", "");
+                                       writer.write_attribute ("Description", "");
+                                       writer.write_attribute ("Director", "");
+                                       writer.write_attribute ("Disks", "");
+                                       writer.write_attribute ("FormattedTitle", "");
+                                       writer.write_attribute ("Languages", "");
+                                       writer.write_attribute ("Length", ""); // (m.runtime + 30) / 60;
+                                       writer.write_attribute ("Number", "");
+                                       writer.write_attribute ("OriginalTitle", movie.title);
+                                       writer.write_attribute ("Picture", "");
+                                       writer.write_attribute ("Rating", (movie.rating / 10).to_string ());
+                                       writer.write_attribute ("Subtitles", "");
+                                       writer.write_attribute ("TranslatedTitle", "");
+                                       writer.write_attribute ("VideoFormat", "");
+                                       writer.write_attribute ("Year", (movie.year > 0) ? movie.year.to_string () : "");
+                                       writer.end_element ();
+                               }
+                               writer.end_element ();
+                       writer.end_element ();
+               writer.end_element ();
+               writer.flush ();
+               return true;
+       }
+}
+