From 11df3a2d0bafab9afba14797dd5972379b630e05 Mon Sep 17 00:00:00 2001 From: Jon Egerton Date: Fri, 15 Aug 2014 14:59:40 +0100 Subject: [PATCH] Implement sorting in the subscriptions list Implement sorting the list of subscriptions in the same way as the downloads list. This resolves #125. --- AUTHORS.md | 3 ++ Classes/Model/Subscription.cs | 73 ++++++++++++++++++++++++++++++++++- Classes/Settings.cs | 26 +++++++++++++ Forms/Main.Designer.cs | 1 + Forms/Main.cs | 29 ++++++++++++++ 5 files changed, 130 insertions(+), 2 deletions(-) diff --git a/AUTHORS.md b/AUTHORS.md index c201ad90..a47df572 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -6,5 +6,8 @@ The following people have contributed code to Radio Downloader: ### Matt Robinson Primary Developer +### Jon Egerton +Sorting in subscriptions list + ### Nick Sharples Original proof of concept code for RSS server diff --git a/Classes/Model/Subscription.cs b/Classes/Model/Subscription.cs index 33afe577..3d01f734 100644 --- a/Classes/Model/Subscription.cs +++ b/Classes/Model/Subscription.cs @@ -1,6 +1,6 @@ /* * This file is part of Radio Downloader. - * Copyright © 2007-2012 by the authors - see the AUTHORS file for details. + * Copyright © 2007-2014 by the authors - see the AUTHORS file for details. * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General * Public License as published by the Free Software Foundation, either version 3 of the License, or (at your @@ -20,6 +20,7 @@ namespace RadioDld.Model using System.Collections.Generic; using System.Data.SQLite; using System.Globalization; + using System.IO; using System.Threading; internal class Subscription : Programme @@ -27,6 +28,9 @@ internal class Subscription : Programme private static Dictionary sortCache; private static object sortCacheLock = new object(); + private static SubscriptionCols sortBy = SubscriptionCols.ProgrammeName; + private static bool sortAsc; + static Subscription() { Programme.Updated += Programme_Updated; @@ -56,6 +60,55 @@ public Subscription(int progid) public static event ProgrammeEventHandler Removed; + internal enum SubscriptionCols + { + ProgrammeName = 0, + LastDownload = 1, + Provider = 2 + } + + public static SubscriptionCols SortByColumn + { + get + { + return sortBy; + } + + set + { + lock (sortCacheLock) + { + if (value != sortBy) + { + sortCache = null; + } + + sortBy = value; + } + } + } + + public static bool SortAscending + { + get + { + return sortAsc; + } + + set + { + lock (sortCacheLock) + { + if (value != sortAsc) + { + sortCache = null; + } + + sortAsc = value; + } + } + } + public static List FetchAll() { List items = new List(); @@ -124,8 +177,24 @@ public static int Compare(int progid1, int progid2) sortCache = new Dictionary(); int sort = 0; + string orderBy = null; + + switch (sortBy) + { + case SubscriptionCols.ProgrammeName: + orderBy = "name" + (sortAsc ? string.Empty : " desc"); + break; + case SubscriptionCols.LastDownload: + orderBy = "latestdownload" + (sortAsc ? string.Empty : " desc"); + break; + case SubscriptionCols.Provider: + orderBy = "pluginid" + (sortAsc ? " desc" : string.Empty); + break; + default: + throw new InvalidDataException("Invalid column: " + sortBy.ToString()); + } - using (SQLiteCommand command = new SQLiteCommand("select subscriptions.progid from subscriptions, programmes where programmes.progid=subscriptions.progid order by name", FetchDbConn())) + using (SQLiteCommand command = new SQLiteCommand("select subscriptions.progid from subscriptions, programmes where programmes.progid=subscriptions.progid order by " + orderBy, FetchDbConn())) { using (SQLiteMonDataReader reader = new SQLiteMonDataReader(command.ExecuteReader())) { diff --git a/Classes/Settings.cs b/Classes/Settings.cs index 1842f360..87424868 100644 --- a/Classes/Settings.cs +++ b/Classes/Settings.cs @@ -102,6 +102,32 @@ internal static bool ShownTrayBalloon } } + internal static Model.Subscription.SubscriptionCols SubscriptionColSortBy + { + get + { + return (Model.Subscription.SubscriptionCols)GetValue("SubscriptionColSortBy", (int)Model.Subscription.SubscriptionCols.ProgrammeName); + } + + set + { + SetValue("SubscriptionColSortBy", (int)value); + } + } + + internal static bool SubscriptionColSortAsc + { + get + { + return GetValue("SubscriptionColSortAsc", true); + } + + set + { + SetValue("SubscriptionColSortAsc", value); + } + } + internal static string DownloadCols { get diff --git a/Forms/Main.Designer.cs b/Forms/Main.Designer.cs index ba465d2b..f682831f 100644 --- a/Forms/Main.Designer.cs +++ b/Forms/Main.Designer.cs @@ -648,6 +648,7 @@ private void InitializeComponent() this.ListSubscribed.TabIndex = 4; this.ListSubscribed.UseCompatibleStateImageBehavior = false; this.ListSubscribed.View = System.Windows.Forms.View.Details; + this.ListSubscribed.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.ListSubscribed_ColumnClick); this.ListSubscribed.ItemActivate += new System.EventHandler(this.ListSubscribed_ItemActivate); this.ListSubscribed.SelectedIndexChanged += new System.EventHandler(this.ListSubscribed_SelectedIndexChanged); // diff --git a/Forms/Main.cs b/Forms/Main.cs index 18bfefb9..870d9a9b 100644 --- a/Forms/Main.cs +++ b/Forms/Main.cs @@ -549,6 +549,30 @@ private void ListSubscribed_SelectedIndexChanged(object sender, EventArgs e) } } + private void ListSubscribed_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e) + { + Model.Subscription.SubscriptionCols clickedCol = (Model.Subscription.SubscriptionCols)e.Column; + + if (Model.Subscription.SortByColumn != clickedCol) + { + Model.Subscription.SortByColumn = clickedCol; + Model.Subscription.SortAscending = true; + } + else + { + Model.Subscription.SortAscending = !Model.Subscription.SortAscending; + } + + // Set the column header to display the new sort order + this.ListSubscribed.ShowSortOnHeader((int)Model.Subscription.SortByColumn, Model.Subscription.SortAscending ? SortOrder.Ascending : SortOrder.Descending); + + // Save the current sort + Settings.SubscriptionColSortBy = Model.Subscription.SortByColumn; + Settings.SubscriptionColSortAsc = Model.Subscription.SortAscending; + + this.ListSubscribed.Sort(); + } + private void ShowSubscriptionInfo(int progid) { Model.Subscription info = new Model.Subscription(progid); @@ -2304,6 +2328,11 @@ private void InitSubscriptionList() this.SetViewDefaults(); // Revert back to default sidebar and toolbar } + // Apply the sort from the current settings + Model.Subscription.SortByColumn = Settings.SubscriptionColSortBy; + Model.Subscription.SortAscending = Settings.SubscriptionColSortAsc; + this.ListSubscribed.ShowSortOnHeader((int)Model.Subscription.SortByColumn, Model.Subscription.SortAscending ? SortOrder.Ascending : SortOrder.Descending); + // Convert the list of Subscription items to an array of ListItems List initData = Model.Subscription.FetchAll(); ListViewItem[] initItems = new ListViewItem[initData.Count];