コード例 #1
0
        protected override void ImportCore ()
        {
            LibraryImportManager import_manager = ServiceManager.Get<LibraryImportManager> ();
            HyenaSqliteConnection conn;

            try {
                conn = new HyenaSqliteConnection (amarok_db_path);
            } catch (Exception e) {
                LogError (amarok_db_path, String.Format (
                    "Unable to open Amarok database: {0}", e.Message));
                return;
            }

            int count = 0;
            try {
                count = conn.Query<int> ("SELECT COUNT(*) FROM tags");
            } catch (Exception) {}

            try {
                HyenaSqliteCommand cmd = new HyenaSqliteCommand (@"
                    SELECT DISTINCT NULL,
                           tags.url,
                           tags.title,
                           artist.name,
                           genre.name,
                           album.name,
                           year.name,
                           tags.track,
                           tags.length
                     FROM  tags,
                           artist,
                           album,
                           genre,
                           year
                     WHERE tags.deviceid = -1
                       AND tags.artist = artist.id
                       AND tags.album = album.id
                       AND tags.genre = genre.id
                       AND tags.year = year.id"
                );

                HyenaSqliteCommand stats_cmd = new HyenaSqliteCommand (@"
                                                     SELECT DISTINCT (rating+rating%2)/2, playcounter, createdate, accessdate
                                                     FROM   statistics
                                                     WHERE  url = ? AND deviceid = -1");

                int processed = 0;

                IDataReader reader = conn.Query (cmd);
                while (reader.Read ()) {
                     if (CheckForCanceled ())
                         break;

                     processed++;

                     try {
                         string path = (string) reader[1];

                         SafeUri uri = null;
                         if (path.StartsWith ("./")) {
                             uri = new SafeUri (path.Substring (1));
                         } else if (path.StartsWith ("/")) {
                             uri = new SafeUri (path);
                         } else {
                             continue;
                         }

                         string title = (string) reader[2];
                         string artist = (string) reader[3];
                         //Console.WriteLine ("Amarok import has {0}/{1} - {2}", artist, title, uri);

                         // the following fields are not critical and can be skipped if something goes wrong
                         int rating = 0, playcount = 0;
                         long created = 0, accessed = 0;

                         // Try to read stats
                         try {
                             IDataReader stats_reader = conn.Query (stats_cmd, path);

                             while (stats_reader.Read ()) {
                                 rating = Convert.ToInt32 (stats_reader[0]);
                                 playcount = Convert.ToInt32 (stats_reader[1]);
                                 created = Convert.ToInt64 (stats_reader[2]);
                                 accessed = Convert.ToInt64 (stats_reader[3]);
                             }
                             stats_reader.Dispose ();
                         } catch (Exception) {}

                         UpdateUserJob (processed, count, artist, title);

                         try {
                             DatabaseTrackInfo track = import_manager.ImportTrack (uri);

                             if (track == null) {
                                 throw new Exception (String.Format (Catalog.GetString ("Unable to import track: {0}"), uri.AbsoluteUri));
                             }

                             if (rating > 0 || playcount > 0 || created > 0 || accessed > 0) {
                                 track.Rating = rating;
                                 track.PlayCount = playcount;
                                 if (created > 0)
                                     track.DateAdded = Hyena.DateTimeUtil.FromTimeT (created);
                                 if (accessed > 0)
                                     track.LastPlayed = Hyena.DateTimeUtil.FromTimeT (accessed);
                                 track.Save (false);
                             }
                         } catch (Exception e) {
                             LogError (SafeUri.UriToFilename (uri), e);
                         }
                     } catch (Exception e) {
                         Hyena.Log.Exception (e);
                         // something went wrong, skip entry
                     }
                 }
                 reader.Dispose ();
                 import_manager.NotifyAllSources ();

                 // TODO migrating more than the podcast subscriptions (eg whether to auto sync them etc) means 1) we need to have those features
                 // and 2) we need to depend on Migo and/or the Podcast extension
                 DBusCommandService cmd_service = ServiceManager.Get<DBusCommandService> ();
                 if (cmd_service != null && ServiceManager.DbConnection.TableExists ("PodcastSyndications")) {
                     foreach (string podcast_url in conn.QueryEnumerable<string> ("SELECT url FROM podcastchannels")) {
                         cmd_service.PushFile (podcast_url.Replace ("http:", "feed:"));
                     }
                 }

            } catch (Exception e) {
                Hyena.Log.Exception (e);
                LogError (amarok_db_path, Catalog.GetString ("Importing from Amarok failed"));
            } finally {
                conn.Dispose ();
            }
        }
コード例 #2
0
        /// <summary>
        /// Compress banshee database if it is not already compressed.
        /// </summary>
        /// This won't do anything if last compression is not more than  _DB_CACHED_COMPRESSION
        /// senconds ago. To avoid that you can reset _dbCompressTime to 0.
        ///
        /// See https://github.com/Knickedi/banshee-remote for more.
        public static void CompressDatabase()
        {
            if (Timestamp() - _dbCompressTime < _DB_CACHED_COMPRESSION)
            {
                return;
            }

            try {
                _dbCompressTime = Timestamp();

                File.Delete(DatabasePath(true));
                File.Delete(DatabasePath(true) + "-journal");
                File.Copy(DatabasePath(false), DatabasePath(true));

                // database was in use, unlock it or queries will just fail
                HyenaSqliteConnection db = new HyenaSqliteConnection(DatabasePath(true));
                db.BeginTransaction();
                db.CommitTransaction();
                db.Dispose();

                db = new HyenaSqliteConnection(DatabasePath(true));
                db.BeginTransaction();

                IDataReader cursor = db.Query(
                    "SELECT tbl_name FROM sqlite_master WHERE "
                    + "type='table' AND tbl_name NOT IN "
                    + "('CoreTracks', 'CoreArtists', 'CoreAlbums', 'sqlite_stat1', 'sqlite_stat2');");

                // drop unnecessary tables
                while (cursor.Read())
                {
                    db.Execute("DROP TABLE " + cursor.Get(0, typeof(string)) + ";");
                }

                // clear analytic data (if available)
                if (db.TableExists("sqlite_stat1"))
                {
                    db.Execute("DELETE FROM sqlite_stat1;");
                }
                if (db.TableExists("sqlite_stat2"))
                {
                    db.Execute("DELETE FROM sqlite_stat2;");
                }

                cursor.Dispose();

                // remove unecessary columns from tracks table
                db.Execute("CREATE TABLE tracks (\n"
                           + "	_id INTEGER PRIMARY KEY,\n"
                           + "	artistId  INTEGER,\n"
                           + "	albumId  INTEGER,\n"
                           + "	title TEXT,\n"
                           + " trackNumber INTEGER,\n"
                           + "	duration INTEGER,\n"
                           + "	year INTEGER,\n"
                           + "	genre TEXT,\n"
                           + " rating INTEGER\n"
                           + ");");
                db.Execute("INSERT INTO tracks(_id, artistId, albumId, "
                           + "title, trackNumber, duration, year, genre, rating) "
                           + "SELECT TrackID, ArtistID, AlbumId, "
                           + "Title, TrackNumber, Duration, Year, Genre, Rating "
                           + "FROM CoreTracks;");

                // remove unecessary columns from artist table
                db.Execute("CREATE TABLE artists (\n"
                           + "	_id INTEGER PRIMARY KEY,\n"
                           + "	name TEXT\n"
                           + ");");
                db.Execute("INSERT INTO artists(_id, name) "
                           + "SELECT ArtistID, Name FROM CoreArtists;");


                // remove unecessary columns from album table
                db.Execute("CREATE TABLE albums (\n"
                           + "	_id INTEGER PRIMARY KEY,\n"
                           + "	artistId INTEGER,\n"
                           + " title TEXT,\n"
                           + " artId TEXT\n"
                           + ");");

                if (db.ColumnExists("CoreAlbums", "ArtworkID"))
                {
                    db.Execute("INSERT INTO albums(_id, artistId, title, artId) "
                               + "SELECT AlbumID, ArtistID, Title, ArtworkID FROM CoreAlbums;");
                }
                else
                {
                    // old banshee versions don't cache the cover ID - fix that manually

                    Dictionary <int, string> artists = new Dictionary <int, string>();
                    IDataReader r = db.Query("SELECT ArtistId, Name FROM CoreArtists;");

                    while (r.Read())
                    {
                        artists.Add(r.Get <int>(0), r.Get <string>(1));
                    }

                    r.Dispose();
                    r = db.Query("SELECT AlbumID, ArtistID, Title FROM CoreAlbums;");

                    while (r.Read())
                    {
                        int    artistId = r.Get <int>(1);
                        string album    = r.Get <string>(2);
                        string artId    = "";
                        string artist   = null;

                        if (artists.TryGetValue(artistId, out artist))
                        {
                            artId = CoverArtSpec.CreateArtistAlbumId(artist ?? "", album ?? "").Replace("'", "''");
                        }

                        db.Execute("INSERT INTO albums(_id, artistId, title, artId) VALUES ("
                                   + r.Get <int>(0) + "," + artistId + ","
                                   + "'" + (album ?? "").Replace("'", "''") + "', "
                                   + "'" + artId + "');");
                    }

                    r.Dispose();
                }

                db.Execute("DROP TABLE CoreTracks;");
                db.Execute("DROP TABLE CoreArtists;");
                db.Execute("DROP TABLE CoreAlbums;");

                db.CommitTransaction();
                db.Execute("VACUUM;");
                db.Dispose();

                SetDbCompressTimeFromFile();
            } catch (Exception e) {
                Log.Error("remote listener failed to compress database: " + e.Message);
                File.Delete(DatabasePath(true));
            }
        }
        protected override void ImportCore()
        {
            LibraryImportManager  import_manager = ServiceManager.Get <LibraryImportManager> ();
            HyenaSqliteConnection conn;

            try {
                conn = new HyenaSqliteConnection(amarok_db_path);
            } catch (Exception e) {
                LogError(amarok_db_path, String.Format(
                             "Unable to open Amarok database: {0}", e.Message));
                return;
            }

            int count = 0;

            try {
                count = conn.Query <int> ("SELECT COUNT(*) FROM tags");
            } catch (Exception) {}

            try {
                HyenaSqliteCommand cmd = new HyenaSqliteCommand(@"
                    SELECT DISTINCT NULL,
                           tags.url,
                           tags.title,
                           artist.name,
                           genre.name,
                           album.name,
                           year.name,
                           tags.track,
                           tags.length
                     FROM  tags,
                           artist,
                           album,
                           genre,
                           year
                     WHERE tags.deviceid = -1
                       AND tags.artist = artist.id
                       AND tags.album = album.id
                       AND tags.genre = genre.id
                       AND tags.year = year.id"
                                                                );

                HyenaSqliteCommand stats_cmd = new HyenaSqliteCommand(@"
                                                     SELECT DISTINCT (rating+rating%2)/2, playcounter, createdate, accessdate
                                                     FROM   statistics
                                                     WHERE  url = ? AND deviceid = -1");

                int processed = 0;

                IDataReader reader = conn.Query(cmd);
                while (reader.Read())
                {
                    if (CheckForCanceled())
                    {
                        break;
                    }

                    processed++;

                    try {
                        string path = (string)reader[1];

                        SafeUri uri = null;
                        if (path.StartsWith("./"))
                        {
                            uri = new SafeUri(path.Substring(1));
                        }
                        else if (path.StartsWith("/"))
                        {
                            uri = new SafeUri(path);
                        }
                        else
                        {
                            continue;
                        }

                        string title  = (string)reader[2];
                        string artist = (string)reader[3];
                        //Console.WriteLine ("Amarok import has {0}/{1} - {2}", artist, title, uri);

                        // the following fields are not critical and can be skipped if something goes wrong
                        int  rating = 0, playcount = 0;
                        long created = 0, accessed = 0;

                        // Try to read stats
                        try {
                            IDataReader stats_reader = conn.Query(stats_cmd, path);

                            while (stats_reader.Read())
                            {
                                rating    = Convert.ToInt32(stats_reader[0]);
                                playcount = Convert.ToInt32(stats_reader[1]);
                                created   = Convert.ToInt64(stats_reader[2]);
                                accessed  = Convert.ToInt64(stats_reader[3]);
                            }
                            stats_reader.Dispose();
                        } catch (Exception) {}

                        UpdateUserJob(processed, count, artist, title);

                        try {
                            DatabaseTrackInfo track = import_manager.ImportTrack(uri);

                            if (track == null)
                            {
                                throw new Exception(String.Format(Catalog.GetString("Unable to import track: {0}"), uri.AbsoluteUri));
                            }

                            if (rating > 0 || playcount > 0 || created > 0 || accessed > 0)
                            {
                                track.Rating    = rating;
                                track.PlayCount = playcount;
                                if (created > 0)
                                {
                                    track.DateAdded = Hyena.DateTimeUtil.FromTimeT(created);
                                }
                                if (accessed > 0)
                                {
                                    track.LastPlayed = Hyena.DateTimeUtil.FromTimeT(accessed);
                                }
                                track.Save(false);
                            }
                        } catch (Exception e) {
                            LogError(SafeUri.UriToFilename(uri), e);
                        }
                    } catch (Exception e) {
                        Hyena.Log.Exception(e);
                        // something went wrong, skip entry
                    }
                }
                reader.Dispose();
                import_manager.NotifyAllSources();

                // TODO migrating more than the podcast subscriptions (eg whether to auto sync them etc) means 1) we need to have those features
                // and 2) we need to depend on Migo and/or the Podcast extension
                DBusCommandService cmd_service = ServiceManager.Get <DBusCommandService> ();
                if (cmd_service != null && ServiceManager.DbConnection.TableExists("PodcastSyndications"))
                {
                    foreach (string podcast_url in conn.QueryEnumerable <string> ("SELECT url FROM podcastchannels"))
                    {
                        cmd_service.PushFile(podcast_url.Replace("http:", "feed:"));
                    }
                }
            } catch (Exception e) {
                Hyena.Log.Exception(e);
                LogError(amarok_db_path, Catalog.GetString("Importing from Amarok failed"));
            } finally {
                conn.Dispose();
            }
        }
コード例 #4
0
 public void Dispose()
 {
     connection.Dispose();
     File.Delete("test.db");
 }