private static void RefreshSettings()
        {
            using (Youtube2Mp3DatabaseEntities db = new Youtube2Mp3DatabaseEntities())
            {
                int count = db.Settings.Where(o => o.Id >= 0).Count();
                if (count == 0)
                {
                    settings = new Settings();
                    settings.AutomaticDownload = "false";
                    settings.MP3Path           = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
                    settings.VideoPath         = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
                    settings.SoundFile         = "";
                    settings.PlaySound         = "true";
                    settings.KeepMp4           = "false";
                    settings.ShowNotification  = "true";
                    settings.CopyFromClipboard = "false";

                    UpdateSettings(settings);
                }
                else
                {
                    settings = (from s in db.Settings select s).ToList().FirstOrDefault();
                }
                db.Dispose();
            }
        }
        /// <summary>
        /// This method will insert every missing column for each table into the database. Will only be called if HasallColumns() returns false. This means the user has an outdated .db file
        /// </summary>
        public static void InsertNewColumns()
        {
            using (Youtube2Mp3DatabaseEntities db = new Youtube2Mp3DatabaseEntities())
            {
                //every column that SHOULD exist
                var downloadHistoryNames = typeof(DownloadHistory).GetProperties().Select(property => property.Name).ToArray();
                var settingNames         = typeof(Settings).GetProperties().Select(property => property.Name).ToArray();

                foreach (string column in downloadHistoryNames)
                {
                    if (!HasColumn(column, "DownloadHistory"))
                    {
                        db.Database.ExecuteSqlCommand("ALTER TABLE DownloadHistory ADD COLUMN " + column + " " + GetDownloadHistoryColumnSqlType(column));
                    }
                }

                foreach (string column in settingNames)
                {
                    if (!HasColumn(column, "settings"))
                    {
                        db.Database.ExecuteSqlCommand("ALTER TABLE SETTINGS ADD COLUMN " + column + " " + GetSettingColumnSqlType(column));
                    }
                }

                db.SaveChanges();
                db.Dispose();
            }
        }
        /// <summary>
        /// Inserts all missing tables into the user's .db file
        /// </summary>
        public static void InsertMissingTables()
        {
            using (Youtube2Mp3DatabaseEntities db = new Youtube2Mp3DatabaseEntities())
            {
                SQLiteConnection conn = new SQLiteConnection();
                conn.ConnectionString = "data source = " + DB_FILE;
                conn.Open();

                SQLiteCommand tableDownloadHistory = new SQLiteCommand();
                SQLiteCommand tableSettings        = new SQLiteCommand();


                tableDownloadHistory.CommandText = TABLE_DOWNLOAD_HISTORY;
                tableSettings.CommandText        = TABLE_SETTINGS;


                tableDownloadHistory.Connection = conn;
                tableSettings.Connection        = conn;


                if (!HasTable("DownloadHistory", db))
                {
                    tableDownloadHistory.ExecuteNonQuery();
                }

                if (!HasTable("Settings", db))
                {
                    tableSettings.ExecuteNonQuery();
                }

                conn.Close();
                conn.Dispose();
                db.Dispose();
            }
        }
 private static void RefreshCacheList()
 {
     using (Youtube2Mp3DatabaseEntities db = new Youtube2Mp3DatabaseEntities())
     {
         localHistory = (from s in db.DownloadHistory select s).ToList();
         db.Dispose();
     }
 }
 /// <summary>
 /// Removes a record from the database
 /// </summary>
 /// <param name="record">the record you want to remove</param>
 public static void RemoveRecord(DownloadHistory historyRecord)
 {
     using (Youtube2Mp3DatabaseEntities db = new Youtube2Mp3DatabaseEntities())
     {
         db.DownloadHistory.Attach(historyRecord);
         db.DownloadHistory.Remove(historyRecord);
         SaveAndCloseDataBase(db);
     }
 }
 /// <summary>
 /// Checks if the database has the given table
 /// </summary>
 /// <param name="table"></param>
 /// <param name="db"></param>
 /// <returns></returns>
 private static bool HasTable(string table, Youtube2Mp3DatabaseEntities db)
 {
     try
     {
         var result = db.Database.ExecuteSqlCommand("select * from " + table);
         return(true);
     }
     catch (SQLiteException)
     {
         return(false);
     }
 }
 /// <summary>
 /// Checks if the user's .db file has all the tables from the database model.
 /// </summary>
 /// <returns></returns>
 public static bool HasAllTables()
 {
     using (Youtube2Mp3DatabaseEntities db = new Youtube2Mp3DatabaseEntities())
     {
         if (HasTable("settings", db) && HasTable("downloadhistory", db))
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
        /// <summary>
        /// Insert a record into the database
        /// </summary>
        /// <param name="record">The record</param>
        public static void InsertRecord(DownloadHistory historyRecord)
        {
            using (Youtube2Mp3DatabaseEntities db = new Youtube2Mp3DatabaseEntities())
            {
                if (db.DownloadHistory.Count() > 0)
                {
                    historyRecord.Id = db.DownloadHistory.Max(i => i.Id) + 1;
                }
                else
                {
                    historyRecord.Id = 0;
                }

                db.DownloadHistory.Add(historyRecord);
                SaveAndCloseDataBase(db);
            }
        }
        /// <summary>
        /// Removes multiple records from the database
        /// </summary>
        /// <param name="record">the record you want to remove</param>
        public static void RemoveRecords(List <DownloadHistory> historyRecords)
        {
            using (Youtube2Mp3DatabaseEntities db = new Youtube2Mp3DatabaseEntities())
            {
                //Go through the loop and add all the remove requests to the list
                foreach (DownloadHistory record in historyRecords)
                {
                    if (GetHistoryEntryById(record.Id) != null)
                    {
                        db.DownloadHistory.Attach(record);
                        db.DownloadHistory.Remove(record);
                    }
                }

                //Save all the remove requests and remove them from the database
                SaveAndCloseDataBase(db);
            }
        }
示例#10
0
        public static bool KeepMp4()
        {
            string keep = "";

            using (Youtube2Mp3DatabaseEntities db = new Youtube2Mp3DatabaseEntities())
            {
                var count = db.Settings.Where(o => o.Id >= 0).Count();
                if (count > 0)
                {
                    keep = (from g in db.Settings select g.KeepMp4).SingleOrDefault();
                    db.Dispose();
                }
                else
                {
                    RefreshSettings();
                }
            }
            return(keep == "true");
        }
示例#11
0
        public static bool IsPlaySound()
        {
            string playSound = "";

            using (Youtube2Mp3DatabaseEntities db = new Youtube2Mp3DatabaseEntities())
            {
                var count = db.Settings.Where(o => o.Id >= 0).Count();
                if (count > 0)
                {
                    playSound = (from g in db.Settings select g.PlaySound).SingleOrDefault();
                    db.Dispose();
                }
                else
                {
                    RefreshSettings();
                }
            }
            return(playSound == "true");
        }
示例#12
0
        public static string GetSoundFile()
        {
            string file = "";

            using (Youtube2Mp3DatabaseEntities db = new Youtube2Mp3DatabaseEntities())
            {
                var count = db.Settings.Where(o => o.Id >= 0).Count();
                if (count > 0)
                {
                    file = (from g in db.Settings select g.SoundFile).SingleOrDefault();
                    db.Dispose();
                }
                else
                {
                    RefreshSettings();
                }
            }
            return(file);
        }
示例#13
0
        public static string GetMP3Path()
        {
            string path = "";

            using (Youtube2Mp3DatabaseEntities db = new Youtube2Mp3DatabaseEntities())
            {
                var count = db.Settings.Where(o => o.Id >= 0).Count();
                if (count > 0)
                {
                    path = (from g in db.Settings select g.MP3Path).SingleOrDefault();
                    db.Dispose();
                }
                else
                {
                    RefreshSettings();
                }
            }
            return(path);
        }
示例#14
0
        public static string GetVideoType()
        {
            string type = "";

            using (Youtube2Mp3DatabaseEntities db = new Youtube2Mp3DatabaseEntities())
            {
                var count = db.Settings.Where(o => o.Id >= 0).Count();
                if (count > 0)
                {
                    type = (from g in db.Settings select g.VideoFormat).SingleOrDefault();
                    db.Dispose();
                }
                else
                {
                    RefreshSettings();
                }
            }
            return(type);
        }
示例#15
0
        public static bool IsShowNotification()
        {
            string notification = "";

            using (Youtube2Mp3DatabaseEntities db = new Youtube2Mp3DatabaseEntities())
            {
                var count = db.Settings.Where(o => o.Id >= 0).Count();
                if (count > 0)
                {
                    notification = (from g in db.Settings select g.ShowNotification).SingleOrDefault();
                    db.Dispose();
                }
                else
                {
                    RefreshSettings();
                }
            }
            return(notification == "true");
        }
示例#16
0
 /// <summary>
 /// Checks wether the table x has column x
 /// </summary>
 /// <param name="columnName">The column you want to check on</param>
 /// <param name="table">The table you want to check on</param>
 /// <returns></returns>
 public static bool HasColumn(string columnName, string table)
 {
     using (Youtube2Mp3DatabaseEntities db = new Youtube2Mp3DatabaseEntities())
     {
         try
         {
             var t = db.Database.SqlQuery <object>("SELECT " + columnName + " FROM " + table).ToList();
             db.Dispose();
             return(true);
         }
         catch (SQLiteException ex)
         {
             db.Dispose();
             //if (ex.Message.ToLower().Contains("no such column"))
             //{
             return(false);
             //}
         }
     }
 }
示例#17
0
 public static void UpdateSettings(Settings set)
 {
     using (Youtube2Mp3DatabaseEntities db = new Youtube2Mp3DatabaseEntities())
     {
         var count = db.Settings.Where(o => o.Id >= 0).Count();
         if (count > 0)
         {
             db.Settings.Attach(set);
             var entry = db.Entry(set);
             entry.State = System.Data.Entity.EntityState.Modified; //Mark it for update
             db.SaveChanges();                                      //push to database
             db.Dispose();
         }
         else
         {//The settings table is still empty
             db.Settings.Add(set);
             db.SaveChanges();
             db.Dispose();
         }
     }
 }
示例#18
0
 /// <summary>
 /// Saves pending changes to the database, disposes it, and refreshes the local cache list
 /// </summary>
 /// <param name="db"></param>
 private static void SaveAndCloseDataBase(Youtube2Mp3DatabaseEntities db)
 {
     db.SaveChanges();
     RefreshCacheList();
     db.Dispose();
 }