From 664235b1eda3f5a0abeac317175a30f1eaab12b4 Mon Sep 17 00:00:00 2001 From: hbeham Date: Wed, 22 Apr 2015 09:51:21 +0200 Subject: [PATCH] support for Samsung J-Series DVB-S channel list --- ChanSort.Api/Model/ChannelList.cs | 2 +- ChanSort.Loader.SamsungJ/DbChannel.cs | 40 ++- ChanSort.Loader.SamsungJ/DbSerializer.cs | 247 ++++++++++-------- .../DbSerializerPlugin.cs | 2 +- ChanSort/MainForm.cs | 2 +- readme.txt | 10 +- 6 files changed, 173 insertions(+), 130 deletions(-) diff --git a/ChanSort.Api/Model/ChannelList.cs b/ChanSort.Api/Model/ChannelList.cs index 1462a0d8..2476b8b5 100644 --- a/ChanSort.Api/Model/ChannelList.cs +++ b/ChanSort.Api/Model/ChannelList.cs @@ -85,7 +85,7 @@ public string AddChannel(ChannelInfo ci) if (!isDupeProgNr) this.channelByProgNr[ci.OldProgramNr] = ci; - var lowerName = ci.Name.ToLower().Trim(); + var lowerName = (ci.Name ?? "").ToLower().Trim(); var byNameList = this.channelByName.TryGet(lowerName); if (byNameList == null) { diff --git a/ChanSort.Loader.SamsungJ/DbChannel.cs b/ChanSort.Loader.SamsungJ/DbChannel.cs index cf008aa4..113b8856 100644 --- a/ChanSort.Loader.SamsungJ/DbChannel.cs +++ b/ChanSort.Loader.SamsungJ/DbChannel.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using System.Data.SQLite; -using System.Text; using ChanSort.Api; namespace ChanSort.Loader.Toshiba @@ -10,23 +9,31 @@ internal class DbChannel : ChannelInfo internal Dictionary OriginalFavIndex = new Dictionary(); #region ctor() - internal DbChannel(SignalSource source, SQLiteDataReader r, IDictionary field, - DataRoot dataRoot) + internal DbChannel(SQLiteDataReader r, IDictionary field, DataRoot dataRoot, Dictionary providers) { - this.SignalSource = source; + var chType = r.GetInt32(field["chType"]); + if (chType == 7) + this.SignalSource = SignalSource.DvbS; + else if (chType == 4) + this.SignalSource = SignalSource.DvbC; + this.RecordIndex = r.GetInt64(field["SRV.srvId"]); this.OldProgramNr = r.GetInt32(field["major"]); - this.Name = this.GetChannelName(r, 2); + this.FreqInMhz = (decimal)r.GetInt32(field["freq"]) / 1000; + this.ChannelOrTransponder = + (this.SignalSource & SignalSource.Cable) != 0 ? LookupData.Instance.GetDvbtTransponder(this.FreqInMhz).ToString() : + (this.SignalSource & SignalSource.Sat) != 0 ? LookupData.Instance.GetTransponderNumber((int)this.FreqInMhz).ToString() : + ""; + this.Name = DbSerializer.ReadUtf16(r, 2); this.Hidden = r.GetBoolean(field["hidden"]); this.Encrypted = r.GetBoolean(field["scrambled"]); this.Lock = r.GetBoolean(field["lockMode"]); this.Skip = !r.GetBoolean(field["numSel"]); - //this.Favorites = this.ParseFavorites(Bits); //if (isAnalog) // this.ReadAnalogData(r, field); //else - this.ReadDvbData(r, field, dataRoot); + this.ReadDvbData(r, field, dataRoot, providers); } #endregion @@ -39,18 +46,19 @@ private void ReadAnalogData(SQLiteDataReader r, IDictionary field) #endregion #region ReadDvbData() - protected void ReadDvbData(SQLiteDataReader r, IDictionary field, DataRoot dataRoot) + protected void ReadDvbData(SQLiteDataReader r, IDictionary field, DataRoot dataRoot, Dictionary providers) { - this.ShortName = this.GetChannelName(r, 3); + this.ShortName = DbSerializer.ReadUtf16(r, 3); this.RecordOrder = r.GetInt32(field["major"]); - this.FreqInMhz = (decimal)r.GetInt32(field["freq"]) / 1000; int serviceType = r.GetInt32(field["srvType"]); this.ServiceType = serviceType; - this.SignalSource = LookupData.Instance.IsRadioOrTv(serviceType); + this.SignalSource |= LookupData.Instance.IsRadioOrTv(serviceType); this.OriginalNetworkId = r.GetInt32(field["onid"]); this.TransportStreamId = r.GetInt32(field["tsid"]); this.ServiceId = r.GetInt32(field["progNum"]); this.VideoPid = r.GetInt32(field["vidPid"]); + if (!r.IsDBNull(field["provId"])) + this.Provider = providers.TryGet(r.GetInt64(field["provId"])); if ((this.SignalSource & SignalSource.Sat) != 0) { //int satId = r.GetInt32(field["sat_id"]); @@ -71,16 +79,6 @@ protected void ReadDvbData(SQLiteDataReader r, IDictionary field, D } #endregion - #region GetChannelNames() - private string GetChannelName(SQLiteDataReader r, int fieldIndex) - { - if (r.IsDBNull(fieldIndex)) - return null; - byte[] nameBytes = new byte[200]; - int nameLen = (int)r.GetBytes(fieldIndex, 0, nameBytes, 0, nameBytes.Length); - return Encoding.BigEndianUnicode.GetString(nameBytes, 0, nameLen); - } - #endregion #region UpdateRawData() public override void UpdateRawData() diff --git a/ChanSort.Loader.SamsungJ/DbSerializer.cs b/ChanSort.Loader.SamsungJ/DbSerializer.cs index fae6119f..034e0ad3 100644 --- a/ChanSort.Loader.SamsungJ/DbSerializer.cs +++ b/ChanSort.Loader.SamsungJ/DbSerializer.cs @@ -3,6 +3,7 @@ using System.Data; using System.Data.SQLite; using System.IO; +using System.Text; using System.Windows.Forms; using ChanSort.Api; using ICSharpCode.SharpZipLib.Zip; @@ -11,17 +12,12 @@ namespace ChanSort.Loader.Toshiba { class DbSerializer : SerializerBase { - private const string FILE_sat = "sat"; - private const string FILE_dvbc = "dvbc"; - //private const string FILE_dvbMainData_db = "dvbMainData.db"; - - private readonly ChannelList atvChannels = new ChannelList(SignalSource.AnalogCT | SignalSource.TvAndRadio, "Analog"); - private readonly ChannelList dvbcChannels = new ChannelList(SignalSource.DvbCT | SignalSource.TvAndRadio, "Cable"); - private readonly ChannelList dvbsChannels = new ChannelList(SignalSource.DvbS | SignalSource.Tv, "Satellite"); private readonly Dictionary channelById = new Dictionary(); - + private readonly Dictionary dbPathByChannelList = new Dictionary(); private string tempDir; + private enum FileType { Unknown, SatDb, ChannelDb } + #region ctor() public DbSerializer(string inputFile) : base(inputFile) { @@ -29,10 +25,6 @@ public DbSerializer(string inputFile) : base(inputFile) this.Features.ChannelNameEdit = false; this.DataRoot.SortedFavorites = true; - - this.DataRoot.AddChannelList(this.atvChannels); - this.DataRoot.AddChannelList(this.dvbcChannels); - this.DataRoot.AddChannelList(this.dvbsChannels); } #endregion @@ -45,44 +37,35 @@ public DbSerializer(string inputFile) : base(inputFile) public override void Load() { this.UnzipDataFile(); - - string channelConnString = "Data Source=" + tempDir + FILE_sat; - using (var conn = new SQLiteConnection(channelConnString)) + foreach (var filePath in Directory.GetFiles(tempDir, "*.")) { - conn.Open(); - using (var cmd = conn.CreateCommand()) + try { - this.RepairCorruptedDatabaseImage(cmd); - this.ReadSatellites(cmd); - this.ReadTransponders(cmd); + using (var conn = new SQLiteConnection("Data Source=" + filePath)) + { + FileType type; + conn.Open(); + using (var cmd = conn.CreateCommand()) + { + this.RepairCorruptedDatabaseImage(cmd); + type = this.DetectFileType(cmd); + } + + switch (type) + { + case FileType.SatDb: + //ReadSatDatabase(conn); + break; + case FileType.ChannelDb: + ReadChannelDatabase(conn, filePath); + break; + } + } } - } - - string sysDataConnString = "Data Source=" + tempDir + FILE_dvbc; - using (var conn = new SQLiteConnection(sysDataConnString)) - { - conn.Open(); - using (var cmd = conn.CreateCommand()) + catch { - this.RepairCorruptedDatabaseImage(cmd); - //this.ReadAnalogChannels(cmd); - this.ReadDtvChannels(cmd); - //this.ReadSatChannels(cmd); - this.ReadFavorites(cmd); } } - -#if false - string mainDataConnString = "Data Source=" + tempDir + FILE_dvbMainData_db; - using (var conn = new SQLiteConnection(mainDataConnString)) - { - conn.Open(); - using (var cmd = conn.CreateCommand()) - { - this.ReadCryptInfo(cmd); - } - } -#endif } #endregion @@ -140,6 +123,33 @@ private void RepairCorruptedDatabaseImage(SQLiteCommand cmd) } #endregion + #region DetectFileType() + private FileType DetectFileType(SQLiteCommand cmd) + { + cmd.CommandText = "select count(1) from sqlite_master where type='table' and name in ('SAT','SAT_TP')"; + if ((long)cmd.ExecuteScalar() == 2) + return FileType.SatDb; + + cmd.CommandText = "select count(1) from sqlite_master where type='table' and name in ('CHNL','SRV','SRV_DVB')"; + if ((long) cmd.ExecuteScalar() == 3) + return FileType.ChannelDb; + + return FileType.Unknown; + } + #endregion + + #region ReadSatDatabase() + private void ReadSatDatabase(SQLiteConnection conn) + { + using (var cmd = conn.CreateCommand()) + { + this.RepairCorruptedDatabaseImage(cmd); + this.ReadSatellites(cmd); + this.ReadTransponders(cmd); + } + } + #endregion + #region ReadSatellites() private void ReadSatellites(SQLiteCommand cmd) { @@ -183,65 +193,79 @@ private void ReadTransponders(SQLiteCommand cmd) } #endregion - #region ReadCryptInfo() - private void ReadCryptInfo(SQLiteCommand cmd) + + #region ReadChannelDatabase() + private void ReadChannelDatabase(SQLiteConnection conn, string dbPath) { - cmd.CommandText = - "select satellite_id, original_network_id, transport_stream_id, service_id, free_CA_mode from services"; - using (var r = cmd.ExecuteReader()) + this.channelById.Clear(); + using (var cmd = conn.CreateCommand()) { - while (r.Read()) - { - int satId = r.IsDBNull(0) ? 0 : r.GetInt32(0); - var sat = this.DataRoot.Satellites.TryGet(satId); - var satPos = sat != null ? sat.OrbitalPosition : "0.0"; - string format = sat != null ? "S{0}-{1}-{2}-{3}" : "C-{1}-{2}-{3}"; - string uid = string.Format(format, satPos, r.GetInt32(1), r.GetInt32(2), r.GetInt32(3)); - } + this.RepairCorruptedDatabaseImage(cmd); + var providers = this.ReadProviders(cmd); + //this.ReadAnalogChannels(cmd); + var channelList = this.ReadDtvChannels(cmd, dbPath, providers); + this.ReadFavorites(cmd); + this.dbPathByChannelList.Add(channelList, dbPath); } } #endregion - - #region ReadAnalogChannels() - private void ReadAnalogChannels(SQLiteCommand cmd) + #region ReadProviders() + private Dictionary ReadProviders(SQLiteCommand cmd) { - string[] fieldNames = {"channel_handle", "channel_number", "list_bits", "channel_label", "frequency"}; - var sql = this.GetQuery("EuroATVChanList", fieldNames); - var fields = this.GetFieldMap(fieldNames); - - cmd.CommandText = sql; - using (var r = cmd.ExecuteReader()) + var dict = new Dictionary(); + try { - while (r.Read()) + cmd.CommandText = "select provId, cast(provName as blob) from PROV"; + using (var r = cmd.ExecuteReader()) { - ChannelInfo channel = new DbChannel(SignalSource.Analog, r, fields, this.DataRoot); - if (!channel.IsDeleted) - this.DataRoot.AddChannel(this.atvChannels, channel); + while (r.Read()) + dict.Add(r.GetInt64(0), ReadUtf16(r, 1)); } } + catch + { + } + return dict; } #endregion - #region ReadDtvChannels() - private void ReadDtvChannels(SQLiteCommand cmd) + #region ReadAnalogChannels() + private void ReadAnalogChannels(SQLiteCommand cmd) { - this.ReadDigitalChannels(cmd, "SRV_DVB", SignalSource.DvbCT, this.dvbcChannels); + //string[] fieldNames = {"channel_handle", "channel_number", "list_bits", "channel_label", "frequency"}; + //var sql = this.GetQuery("EuroATVChanList", fieldNames); + //var fields = this.GetFieldMap(fieldNames); + + //cmd.CommandText = sql; + //using (var r = cmd.ExecuteReader()) + //{ + // while (r.Read()) + // { + // ChannelInfo channel = new DbChannel(SignalSource.Analog, r, fields, this.DataRoot); + // if (!channel.IsDeleted) + // this.DataRoot.AddChannel(this.atvChannels, channel); + // } + //} } #endregion - #region ReadSatChannels() - private void ReadSatChannels(SQLiteCommand cmd) + #region ReadDtvChannels() + private ChannelList ReadDtvChannels(SQLiteCommand cmd, string dbPath, Dictionary providers) { - this.ReadDigitalChannels(cmd, "EuroSATChanList", SignalSource.DvbS, this.dvbsChannels); + string name = Path.GetFileName(dbPath); + ChannelList channelList = new ChannelList(SignalSource.Digital, name); + this.ReadDigitalChannels(cmd, "SRV_DVB", channelList, providers); + this.DataRoot.AddChannelList(channelList); + return channelList; } #endregion #region ReadDigitalChannels() - private void ReadDigitalChannels(SQLiteCommand cmd, string table, SignalSource signalSource, ChannelList channelList) + private void ReadDigitalChannels(SQLiteCommand cmd, string table, ChannelList channelList, Dictionary providers) { - string[] fieldNames = { "SRV.srvId", "major", "cast(srvName as blob)", "cast(shrtSrvName as blob)", "CHNL.chId", - "srvType", "onid", "tsid", "vidPid", "progNum", "freq", "hidden", "scrambled", "lockMode", "numSel" }; + string[] fieldNames = { "SRV.srvId", "major", "cast(srvName as blob)", "cast(shrtSrvName as blob)", "chType", "chNum", + "srvType", "onid", "tsid", "vidPid", "progNum", "freq", "hidden", "scrambled", "lockMode", "numSel", "provId" }; var sql = this.GetQuery(table, fieldNames); var fields = this.GetFieldMap(fieldNames); @@ -250,7 +274,7 @@ private void ReadDigitalChannels(SQLiteCommand cmd, string table, SignalSource s { while (r.Read()) { - var channel = new DbChannel(signalSource, r, fields, this.DataRoot); + var channel = new DbChannel(r, fields, this.DataRoot, providers); if (!channel.IsDeleted) { this.DataRoot.AddChannel(channelList, channel); @@ -296,15 +320,26 @@ private void ReadFavorites(SQLiteCommand cmd) var channel = this.channelById.TryGet(r.GetInt64(0)); if (channel == null) continue; - int fav = r.GetInt32(1); + int fav = r.GetInt32(1) - 1; int pos = r.GetInt32(2); channel.FavIndex[fav] = channel.OriginalFavIndex[fav] = pos; - if (pos > 0) + if (pos >= 0) channel.Favorites |= (Favorites) (1 << fav); } } #endregion + #region ReadUtf16() + internal static string ReadUtf16(SQLiteDataReader r, int fieldIndex) + { + if (r.IsDBNull(fieldIndex)) + return null; + byte[] nameBytes = new byte[200]; + int nameLen = (int)r.GetBytes(fieldIndex, 0, nameBytes, 0, nameBytes.Length); + return Encoding.BigEndianUnicode.GetString(nameBytes, 0, nameLen); + } + #endregion + #region Save() public override void Save(string tvOutputFile) @@ -315,8 +350,29 @@ public override void Save(string tvOutputFile) this.FileName = tvOutputFile; } - string channelConnString = "Data Source=" + this.tempDir + FILE_dvbc; - using (var conn = new SQLiteConnection(channelConnString)) + using (var zip = new ZipFile(this.FileName)) + { + zip.BeginUpdate(); + + foreach (var channelList in this.DataRoot.ChannelLists) + { + var dbPath = this.dbPathByChannelList[channelList]; + SaveChannelList(channelList, dbPath); + + var entryName = Path.GetFileName(dbPath); + zip.Delete(entryName); + zip.Add(dbPath, entryName); + } + + zip.CommitUpdate(); + } + } + #endregion + + #region SaveChannelList() + private void SaveChannelList(ChannelList channelList, string dbPath) + { + using (var conn = new SQLiteConnection("Data Source=" + dbPath)) { conn.Open(); using (var cmd = conn.CreateCommand()) @@ -327,15 +383,14 @@ public override void Save(string tvOutputFile) using (var trans = conn.BeginTransaction()) { this.PrepareCommands(cmd, cmd2, cmd3, cmd4); - this.WriteChannels(cmd, cmd2, cmd3, cmd4, this.dvbcChannels); + this.WriteChannels(cmd, cmd2, cmd3, cmd4, channelList); trans.Commit(); } this.RepairCorruptedDatabaseImage(cmd); } } - - this.ZipFiles(); } + #endregion #region PrepareCommands() @@ -396,33 +451,19 @@ private void WriteChannels(SQLiteCommand cmd, SQLiteCommand cmd2, SQLiteCommand { var c = oldPos < 0 ? cmd2 : cmd3; c.Parameters["@id"].Value = channel.RecordIndex; - c.Parameters["@fav"].Value = i; + c.Parameters["@fav"].Value = i + 1; c.Parameters["@pos"].Value = newPos; c.ExecuteNonQuery(); } else { cmd4.Parameters["@id"].Value = channel.RecordIndex; - cmd4.Parameters["@fav"].Value = i; + cmd4.Parameters["@fav"].Value = i + 1; cmd4.ExecuteNonQuery(); } } } } #endregion - - #region ZipFiles() - private void ZipFiles() - { - const string entryName = FILE_dvbc; - using (var zip = new ZipFile(this.FileName)) - { - zip.BeginUpdate(); - zip.Delete(entryName); - zip.Add(this.tempDir + FILE_dvbc, entryName); - zip.CommitUpdate(); - } - } - #endregion } } diff --git a/ChanSort.Loader.SamsungJ/DbSerializerPlugin.cs b/ChanSort.Loader.SamsungJ/DbSerializerPlugin.cs index 4a1d3418..60f55342 100644 --- a/ChanSort.Loader.SamsungJ/DbSerializerPlugin.cs +++ b/ChanSort.Loader.SamsungJ/DbSerializerPlugin.cs @@ -5,7 +5,7 @@ namespace ChanSort.Loader.Toshiba public class DbSerializerPlugin : ISerializerPlugin { public string PluginName { get { return "Samsung J-Series"; } } - public string FileFilter { get { return "channel_list*-1200*.zip"; } } + public string FileFilter { get { return "channel_list_t*.zip"; } } public SerializerBase CreateSerializer(string inputFile) { diff --git a/ChanSort/MainForm.cs b/ChanSort/MainForm.cs index eca4788a..a8d46ac6 100644 --- a/ChanSort/MainForm.cs +++ b/ChanSort/MainForm.cs @@ -25,7 +25,7 @@ namespace ChanSort.Ui { public partial class MainForm : XtraForm { - public const string AppVersion = "v2015-04-17"; + public const string AppVersion = "v2015-04-18"; private const int MaxMruEntries = 10; diff --git a/readme.txt b/readme.txt index cd0baea9..dcd20634 100644 --- a/readme.txt +++ b/readme.txt @@ -1,8 +1,8 @@ -Version v2015-04-17 ======================================================= +Version v2015-04-18 ======================================================= Changes: -- added experimental support for Samsung J-Series DVB-C channel lists - (analog channels, DVB-T and DVB-S are not supported yet) +- added support for Samsung J-Series DVB-C and DVB-S channel lists + (analog channels and DVB-T are not supported yet) The complete change log can be found at the end of this document @@ -118,6 +118,10 @@ OTHER DEALINGS IN THE SOFTWARE. Change log ================================================================ +2015-04-18 +- added support for Samsung J-Series DVB-C and DVB-S channel lists + (analog channels and DVB-T are not supported yet) + 2015-04-17 - added experimental support for Samsung J-Series DVB-C channel lists (analog channels, DVB-T and DVB-S are not supported yet)