Skip to content

Commit

Permalink
- Philips ChannelMap_45: TV did not remember last selected favorite l…
Browse files Browse the repository at this point in the history
…ist when first fav list was created by ChanSort.

- Philips ChannelMap_100 and later: "Channel" XML elements inside the DVB*.xml files are now reordered by program nr.
- Philips ChannelMap_105 and 110: fixed saving favorite lists (keeping FavoriteNumber="0" in DVB*.xml and only
  setting the numbers in Favorites.xml)
- m3u: keep original end-of-line characters (CRLF or LF)
- m3u: detect whether channel names are prefixed with a program number or not, and save the file in the same way.
  • Loading branch information
Horst Beham committed Feb 24, 2021
1 parent fb37239 commit bc4b650
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 81 deletions.
14 changes: 14 additions & 0 deletions source/ChanSort.Api/Utils/IniFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ public int[] GetIntList(string key)
}
#endregion

#region GetBool()
public bool GetBool(string key, bool defaultValue = false)
{
var val = GetString(key)?.ToLowerInvariant();
if (val == null)
return defaultValue;
if (val == "false" || val == "off" || val == "no" || val == "0")
return false;
if (val == "true" || val == "on" || val == "yes" || val == "1")
return true;
return defaultValue;
}
#endregion

#region ParseNumber()
private int ParseNumber(string value)
{
Expand Down
12 changes: 10 additions & 2 deletions source/ChanSort.Loader.M3u/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Serializer : SerializerBase

private Encoding overrideEncoding;
private string newLine = "\r\n";
private bool allChannelsPrefixedWithProgNr = true;
private List<string> headerLines = new List<string>();
private List<string> trailingLines = new List<string>(); // comment and blank lines after the last URI line

Expand Down Expand Up @@ -55,7 +56,7 @@ public override void Load()
overrideEncoding = new UTF8Encoding(false);

// detect line separator
int idx = Array.IndexOf(content, '\n');
int idx = Array.IndexOf(content, (byte)'\n');
this.newLine = idx >= 1 && content[idx-1] == '\r' ? "\r\n" : "\n";

var rdr = new StreamReader(new MemoryStream(content), overrideEncoding ?? this.DefaultEncoding);
Expand Down Expand Up @@ -126,6 +127,7 @@ private void ReadChannel(int uriLineNr, string uriLine, string extInfLine, strin

if (extInfLine != null)
{
bool extInfContainsProgNr = false;
extInfTrackNameIndex = FindExtInfTrackName(extInfLine);
if (extInfTrackNameIndex >= 0)
{
Expand All @@ -135,10 +137,13 @@ private void ReadChannel(int uriLineNr, string uriLine, string extInfLine, strin
{
progNr = this.ParseInt(match.Groups[1].Value);
name = match.Groups[2].Value;
extInfContainsProgNr = true;
}
}
this.allChannelsPrefixedWithProgNr &= extInfContainsProgNr;
}


if (progNr == 0)
progNr = this.allChannels.Count + 1;

Expand Down Expand Up @@ -231,7 +236,10 @@ public override void Save(string tvOutputFile)
foreach (var line in chan.Lines)
{
if (line.StartsWith("#EXTINF:"))
file.WriteLine($"{line.Substring(0, chan.ExtInfTrackNameIndex)}{chan.NewProgramNr}. {chan.Name}");
{
var progNrPrefix = this.allChannelsPrefixedWithProgNr ? chan.NewProgramNr + ". " : "";
file.WriteLine($"{line.Substring(0, chan.ExtInfTrackNameIndex)}{progNrPrefix}{chan.Name}");
}
else
file.WriteLine(line);
}
Expand Down
88 changes: 47 additions & 41 deletions source/ChanSort.Loader.Philips/BinarySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ class BinarySerializer : SerializerBase

private ChanLstBin chanLstBin;
private readonly StringBuilder logMessages = new StringBuilder();
private readonly List<int> favListIndexToId = new();
private readonly Dictionary<int, int> favListIdToIndex = new();
private readonly ChannelList favChannels = new ChannelList(SignalSource.All, "Favorites");
private const int FavListCount = 8;
private bool mustFixFavListIds;

#region ctor()
public BinarySerializer(string inputFile) : base(inputFile)
Expand Down Expand Up @@ -647,40 +647,45 @@ private void LoadMap45Favorites(string listDb)
this.favChannels.AddChannel(chan);
}
}
this.Features.SupportedFavorites = (Favorites)0xFF;
this.Features.SortedFavorites = true;
this.Features.MixedSourceFavorites = true;
this.Features.AllowGapsInFavNumbers = false;


using var conn = new SQLiteConnection($"Data Source={listDb}");
conn.Open();

// older versions of ChanSort wrote invalid "list_id" values starting at 0 instead of 1 and going past 8.
// if everything is in the range of 1-8, this code keeps the current ids. otherwise it remaps them to 1-8.
using var cmd = conn.CreateCommand();
cmd.CommandText = "select list_id, list_name from List";
this.Features.SupportedFavorites = 0;
this.Features.SortedFavorites = true;
cmd.CommandText = "select min(list_id), max(list_id) from List";
using (var r = cmd.ExecuteReader())
{
int i = 0;
while (r.Read() && i < ChannelInfo.MAX_FAV_LISTS)
{
this.favListIndexToId.Add(r.GetInt32(0));
this.favListIdToIndex.Add(r.GetInt32(0), i);
this.DataRoot.SetFavListCaption(i, r.GetString(1));
this.Features.SupportedFavorites |= (Favorites) (1 << i);
i++;
}
r.Read();
mustFixFavListIds = !r.IsDBNull(0) && (r.GetInt16(0) < 1 || r.GetInt16(1) > 8);
if (mustFixFavListIds)
logMessages.AppendLine("invalid list_id values in list.db will be corrected");
}

for (; i < 8; i++)
cmd.CommandText = "select list_id, list_name from List order by list_id";
var listIds = new List<int>();
using (var r = cmd.ExecuteReader())
{
var listIndex = 0;
while (r.Read())
{
this.favListIndexToId.Add(-i-1);
this.favListIdToIndex.Add(-i-1, i);
this.DataRoot.SetFavListCaption(i, "Fav " + (i+1));
this.Features.SupportedFavorites |= (Favorites)(1 << i);
var listId = r.GetInt16(0);
listIds.Add(listId);
if (!this.mustFixFavListIds)
listIndex = listId - 1;
this.DataRoot.SetFavListCaption(listIndex, r.GetString(1));
++listIndex;
}
}

for (int listIndex = 0; listIndex < this.favListIndexToId.Count; listIndex++)
for (int listIndex = 0; listIndex < listIds.Count; listIndex++)
{
cmd.CommandText = $"select channel_id from FavoriteChannels where fav_list_id={favListIndexToId[listIndex]} order by rank";
cmd.CommandText = $"select channel_id from FavoriteChannels where fav_list_id={listIds[listIndex]} order by rank";
using var r = cmd.ExecuteReader();
int seq = 0;
while (r.Read())
Expand Down Expand Up @@ -997,29 +1002,30 @@ private void UpdateChannelMap45ListDb()
conn.Open();
using var trans = conn.BeginTransaction();
using var cmd = conn.CreateCommand();
for (int favListIndex = 0; favListIndex < this.favListIndexToId.Count; favListIndex++)
cmd.CommandText = "delete from FavoriteChannels";
cmd.ExecuteNonQuery();
if (this.mustFixFavListIds)
{
var favListId = favListIndexToId[favListIndex];
string sqlInsertOrUpdateList;
if (favListId >= 0)
{
cmd.CommandText = $"delete from FavoriteChannels where fav_list_id={favListId}";
cmd.ExecuteNonQuery();
sqlInsertOrUpdateList = "update List set list_name=@name, list_version=list_version+1 where list_id=@id";
}
else
{
favListId = favListIndexToId.Count == 0 ? 1 : favListIndexToId.Max() + 1;
favListIndexToId[favListIndex] = favListId;
favListIdToIndex[favListId] = favListIndex;
sqlInsertOrUpdateList = "insert into List (list_id, list_name, list_version) values (@id,@name,1)";
}
cmd.CommandText = "delete from List";
cmd.ExecuteNonQuery();
}

var incFavList = (ini.GetSection("Map" + chanLstBin.VersionMajor)?.GetBool("incrementFavListVersion", true) ?? true)
? ", list_version=list_version+1"
: "";

for (int favListIndex = 0; favListIndex < FavListCount; favListIndex++)
{
var favListId = favListIndex + 1;
cmd.CommandText = $"select count(1) from List where list_id={favListId}";
cmd.CommandText = (long) cmd.ExecuteScalar() == 0 ?
"insert into List (list_id, list_name, list_version) values (@id,@name,1)" :
"update List set list_name=@name" + incFavList + " where list_id=@id";

cmd.CommandText = sqlInsertOrUpdateList;
cmd.Parameters.Add(new SQLiteParameter("@id", DbType.Int16));
cmd.Parameters.Add(new SQLiteParameter("@name", DbType.String));
cmd.Parameters["@id"].Value = favListId;
cmd.Parameters["@name"].Value = DataRoot.GetFavListCaption(favListIndex);
cmd.Parameters["@name"].Value = DataRoot.GetFavListCaption(favListIndex) ?? "Fav " + (favListIndex + 1);
cmd.ExecuteNonQuery();

cmd.CommandText = "insert into FavoriteChannels(fav_list_id, channel_id, rank) values (@listId,@channelId,@rank)";
Expand Down Expand Up @@ -1049,7 +1055,7 @@ private void UpdateChannelMap45ListDb()
cmd.ExecuteNonQuery();

// make sure the last_watched_channel_id is valid in the list
cmd.CommandText = "update List set last_watched_channel_id=(select min(channel_id) from FavoriteChannels f where f.fav_list_id=List.list_id)";
cmd.CommandText = @"update List set last_watched_channel_id=(select channel_id from FavoriteChannels f where f.fav_list_id=List.list_id order by rank limit 1) where last_watched_channel_id not in (select channel_id from FavoriteChannels f where f.fav_list_id=List.list_id)";
cmd.ExecuteNonQuery();

trans.Commit();
Expand Down
14 changes: 14 additions & 0 deletions source/ChanSort.Loader.Philips/ChanSort.Loader.Philips.ini
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,17 @@ lenSatName=64
offUnk1=48
offUnk2=68
offPolarity=72

[Map45]
incrementFavListVersion=true

[Map100]
setFavoriteNumber=true

[Map105]
incrementFavListVersion=true
setFavoriteNumber=false

[Map110]
incrementFavListVersion=true
setFavoriteNumber=false
Loading

0 comments on commit bc4b650

Please sign in to comment.