/*
Copyright (c) 2002 Joe Gregorio
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Credits:
Bug#[ 574278 ] Invisible Config... button when resizing
Submitted by: Bernard Vander Beken, Fixed by: Simon Fell
Bug#[ 574175 ] Multiple select - delete doesn't work
Submitted by: Eric Vitiello Jr. (presto) aka pixel, Fixed by: Simon Fell
Feature #[ 574124 ] Sort listView Columns
Submitted by: Eric Vitiello Jr. (presto) aka pixel, Added by: Simon Fell
Bug#[ 573628 ] Can delete a channel when scanning.
Submitted by: Eric Vitiello Jr. (presto) aka pixel, Fixed by: Joe Gregorio
Feature #[ 573713 ] Open Browser Button
Submitted by: Eric Vitiello Jr. (presto) aka pixel, Added by: Simon Fell
Feature #[ 575040 ] View Button Options
Submitted by: Eric Vitiello Jr. (presto) aka pixel, Added by: Joe Gregorio
Bug #570292 Freeze on Merge
Submitted by: Joe Gregorio, Fixed by: Simon Fell
Feature #[ 574452 ] GUI should remember layout
Submitted by: Simon Fell, Added by: Simon Fell
*/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.IO;
using Microsoft.Win32;
namespace Bitworking{
public class AggieForm : Form {
private Button add_;
private Button delete_;
private Button scan_;
private Button open_;
private Button load_;
private TextBox address_;
private TextBox siteListFileName_;
private ListView list_;
private ProgressBar progress_;
private Button config_;
private Button feedView_;
private Aggie news_;
private AggieListViewSorter sorter_ ;
private static System.Windows.Forms.Timer updateTimer_;
public AggieForm() {
news_ = Aggie.CreateFromInitFile();
if (news_.InitErrorMessage != "") {
MessageBox.Show(news_.InitErrorMessage, "Initialization Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
Icon = new Icon("Aggie.ico");
Text = "Aggie";
StartPosition = FormStartPosition.Manual;
Size = new Size(800,320) ;
address_ = new TextBox();
address_.Location = new System.Drawing.Point(10, 13);
address_.Size = new System.Drawing.Size(650, 30);
address_.TabIndex = 1;
address_.Text = "http://";
address_.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
address_.AllowDrop = true;
address_.DragDrop += new DragEventHandler(this.Address_DragDrop);
address_.DragEnter += new DragEventHandler(this.Address_DragEnter);
address_.KeyPress += new KeyPressEventHandler(this.Address_KeyPress);
siteListFileName_ = new TextBox();
siteListFileName_.Location = new System.Drawing.Point(10, 240);
siteListFileName_.Size = new System.Drawing.Size(650, 30);
siteListFileName_.TabIndex = 8;
siteListFileName_.Text = news_.siteListFileName;
siteListFileName_.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom;
siteListFileName_.KeyPress += new KeyPressEventHandler(this.SiteListFileName_KeyPress);
add_ = new Button();
add_.Location = new System.Drawing.Point(670, 10);
add_.Size = new System.Drawing.Size(115, 25);
add_.TabIndex = 2;
add_.Text = "Add Channel";
add_.Anchor = AnchorStyles.Right | AnchorStyles.Top;
add_.Click += new System.EventHandler(this.Add);
scan_ = new Button();
scan_.Location = new System.Drawing.Point(740, 50);
scan_.Size = new System.Drawing.Size(45, 25);
scan_.TabIndex = 5;
scan_.Text = "Go!";
scan_.Anchor = AnchorStyles.Right | AnchorStyles.Top;
scan_.Click += new System.EventHandler(this.Scan);
open_ = new Button();
open_.Location = new System.Drawing.Point(740, 80);
open_.Size = new System.Drawing.Size(45, 25);
open_.TabIndex = 6;
open_.Text = "View";
open_.Anchor = AnchorStyles.Right | AnchorStyles.Top;
open_.Click += new System.EventHandler(this.OpenBrowser);
open_.Enabled = File.Exists("Aggie.html");
progress_ = new ProgressBar();
progress_.Location = new System.Drawing.Point(10, 240);
progress_.Size = new System.Drawing.Size(770, 25);
progress_.TabIndex = 100;
progress_.Minimum = 1;
progress_.Maximum = 100;
progress_.Visible = false;
progress_.Step = 1;
progress_.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom;
delete_ = new Button();
delete_.Location = new System.Drawing.Point(740, 140);
delete_.Size = new System.Drawing.Size(45, 25);
delete_.TabIndex = 7;
delete_.Text = "Delete";
delete_.Anchor = AnchorStyles.Right | AnchorStyles.Top;
delete_.Click += new System.EventHandler(this.Delete);
load_ = new Button();
load_.Location = new System.Drawing.Point(670, 237);
load_.Size = new System.Drawing.Size(115, 25);
load_.TabIndex = 9;
load_.Text = "Load Channel List";
load_.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
load_.Click += new System.EventHandler(this.LoadFileName);
config_ = new Button();
config_.Location = new System.Drawing.Point(740, 207);
config_.Size = new System.Drawing.Size(45, 25);
config_.TabIndex = 10;
config_.Text = "Config";
config_.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
config_.Click += new EventHandler(this.Config_Clicked);
feedView_ = new Button();
feedView_.Location = new System.Drawing.Point(740, 110);
feedView_.Size = new System.Drawing.Size(45, 25);
feedView_.TabIndex = 8;
feedView_.Text = "Feed";
feedView_.Anchor = AnchorStyles.Right | AnchorStyles.Top;
feedView_.Click += new System.EventHandler(this.ViewFeed);
list_ = new ListView();
list_.Location = new System.Drawing.Point(10, 50);
list_.Size = new System.Drawing.Size(720, 180);
list_.TabIndex = 3;
list_.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom;
updateTimer_ = new System.Windows.Forms.Timer();
// Set the view to show details.
list_.View = View.Details;
// Allow the user to edit item text.
list_.LabelEdit = false;
// Allow the user to rearrange columns.
list_.AllowColumnReorder = true;
// Display check boxes.
list_.CheckBoxes = false;
// Select the item and subitems when selection is made.
list_.FullRowSelect = true;
// Display grid lines.
list_.GridLines = true;
// Sort the items in the list in ascending order.
list_.Sorting = SortOrder.Ascending;
list_.ItemActivate += new System.EventHandler(this.ListViewItemSelected);
list_.Activation = ItemActivation.OneClick;
list_.HideSelection = false;
sorter_ = new AggieListViewSorter() ;
list_.ListViewItemSorter = sorter_ ;
list_.ColumnClick += new ColumnClickEventHandler(this.ListViewColumnClicked) ;
AddListViewItems();
Controls.Add(add_);
Controls.Add(address_);
Controls.Add(siteListFileName_);
Controls.Add(list_);
Controls.Add(delete_);
Controls.Add(load_);
Controls.Add(scan_);
Controls.Add(open_);
Controls.Add(progress_);
Controls.Add(config_);
Controls.Add(feedView_);
delete_.Enabled = false;
feedView_.Enabled = false;
if (news_.IsReadOnlySiteList()) {
add_.Enabled = false;
address_.Enabled = false;
delete_.Enabled = false;
}
updateTimer_.Tick += new EventHandler(TimerEventProcessor);
updateTimer_.Interval = 500;
Closing += new CancelEventHandler(OnClosing) ;
reloadFormLayout();
}
private void reloadFormLayout() {
RegistryKey key = layoutRegKey ;
Height = (int)key.GetValue("height", Height) ;
Width = (int)key.GetValue("width", Width) ;
Top = (int)key.GetValue("top", Top) ;
Left = (int)key.GetValue("left", Left) ;
}
private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e) {
// save size & pos
if ( WindowState == FormWindowState.Normal ) {
RegistryKey key = layoutRegKey ;
key.SetValue("left", Left);
key.SetValue("top", Top);
key.SetValue("width", Width);
key.SetValue("height", Height);
}
}
private RegistryKey layoutRegKey {
get { return Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Bitworking.org\Aggie"); }
}
private void Address_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.Text)) {
e.Effect = DragDropEffects.Copy;
} else {
e.Effect = DragDropEffects.None;
}
}
private void Address_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
address_.Text = e.Data.GetData(DataFormats.Text).ToString();
Add_Impl();
}
private void Address_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
if (e.KeyChar == '\r') {
Add_Impl();
}
}
public void Config_Clicked(object o, EventArgs ev) {
ConfigForm f = new ConfigForm(ref news_);
f.ShowDialog();
}
private Site Add_Impl2( string siteName ) {
if (news_.duplicateSiteURL(siteName)) {
MessageBox.Show("You are already subscribed to this channel: " + siteName, "Duplicate Address", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return null;
}
Site s = Bitworking.Site.CreateSiteFromURL(siteName);
return s;
}
private void Add_Impl() {
Cursor.Current = Cursors.WaitCursor;
string siteName = address_.Text;
Exception e = null;
bool tryAgain = true;
try {
Site s = null;
try {
s = Add_Impl2(siteName);
if (null == s) {
tryAgain = false; // No need, because already subscribed
}
}
catch ( Exception e1 ) {
// Save the exception code for later analysis,
// but don't propagate it yet
e = e1;
}
if (null == s && tryAgain) {
s = Add_Impl2("http://" + siteName);
}
if (null != s && s.status == Bitworking.Site.State.Succeeded) {
news_.Add(ref s);
list_.Clear();
AddListViewItems();
news_.WriteToOPML();
}
}
catch (System.Exception e2) {
if ( e == null )
e = e2;
HelpfulException he = new HelpfulException( e );
MessageBox.Show("The channel has the following error:\r\n\r\n" + he.Message, "Unable to retrieve or parse RSS file.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
Cursor.Current = Cursors.Default;
}
public void Add(object o, EventArgs ev) {
Add_Impl();
}
public void ListViewColumnClicked(object o, ColumnClickEventArgs e) {
if ( sorter_.ColumnIndex == e.Column ) {
sorter_.ToggleDirection() ;
} else {
sorter_.ColumnIndex = e.Column ;
}
list_.Sort() ;
}
public void ListViewItemSelected(object o, EventArgs e) {
feedView_.Enabled = true;
if (!news_.IsReadOnlySiteList() && false == progress_.Visible) {
delete_.Enabled = true;
}
}
public void Delete(object o, EventArgs e) {
delete_.Enabled = false;
for ( int idx = 0 ; idx < list_.SelectedItems.Count ; ++idx ) {
Site site = (Site)list_.SelectedItems[idx].Tag;
news_.Remove(site);
}
news_.WriteToOPML();
list_.Clear();
AddListViewItems();
}
private void LoadFileNameImpl() {
delete_.Enabled = false;
string oldFileName = news_.siteListFileName;
Cursor.Current = Cursors.WaitCursor;
try {
news_.siteListFileName = siteListFileName_.Text;
news_.Init();
news_.WriteInitFile();
}
catch (System.Exception e) {
Cursor.Current = Cursors.Default;
MessageBox.Show(e.Message, "Error retrieving list of channels", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
news_.siteListFileName = oldFileName;
siteListFileName_.Text = oldFileName;
news_.Init();
}
finally {
Cursor.Current = Cursors.Default;
add_.Enabled = !news_.IsReadOnlySiteList();
address_.Enabled = !news_.IsReadOnlySiteList();
delete_.Enabled = !news_.IsReadOnlySiteList();
list_.Clear();
AddListViewItems();
}
}
private void SiteListFileName_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
if (e.KeyChar == '\r') {
LoadFileNameImpl();
}
}
public void LoadFileName(object o, EventArgs ev) {
LoadFileNameImpl();
}
public void ViewFeed(object o, EventArgs ev) {
feedView_.Enabled = false;
// Only the first item selected is opened
// TODO: Breaks all possible forms of encapsulation!
if ( list_.SelectedItems.Count > 0 ) {
Site site = (Site)list_.SelectedItems[0].Tag;
try {
Process p = System.Diagnostics.Process.Start("http\\" + site.filename);
} catch ( Exception ) {
MessageBox.Show( "Feed not loaded into the cache yet" );
}
}
}
public void Scan(object o, EventArgs e) {
foreach (Site s in news_.siteList) {
s.Reset();
}
siteListFileName_.Visible = false;
load_.Visible = false;
progress_.Visible = true;
progress_.Value = 1;
add_.Enabled = false;
scan_.Enabled = false;
open_.Enabled = false;
config_.Enabled = false;
delete_.Enabled = false;
feedView_.Enabled = false;
updateTimer_.Start();
}
public void OpenBrowser(object o, EventArgs e) {
OpenBrowser() ;
}
private void OpenBrowser() {
try {
news_.Transform(false);
Process p = System.Diagnostics.Process.Start("Aggie.html");
}
catch (Exception e) {
MessageBox.Show("There is probably an error in the XSL file: " + e.Message, "Failure transforming the data into Aggie.html", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void AddListViewItems() {
delete_.Enabled = false;
list_.Columns.Add("Name", 170, HorizontalAlignment.Left);
list_.Columns.Add("Status", 80, HorizontalAlignment.Left);
list_.Columns.Add("URL", 250, HorizontalAlignment.Left);
list_.Columns.Add("Details", 500, HorizontalAlignment.Left);
try {
foreach (Site s in news_.siteList) {
ListViewItem item1 = new ListViewItem(s.title, 0);
item1.Tag = s;
item1.SubItems.Add(s.status.ToString());
item1.SubItems.Add(s.url);
item1.SubItems.Add(""); // Blank error message
list_.Items.Add(item1);
}
}
catch (System.Exception e) {
MessageBox.Show(e.Message, "Error displaying items", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void MergeEvent ( object source, SiteEventArgs e ) {
progress_.PerformStep();
if (progress_.Value >= progress_.Maximum) {
progress_.Value = progress_.Minimum;
}
}
private void TimerEventProcessor(Object o, EventArgs args) {
updateTimer_.Stop();
bool finished = true;
int numRunning = 0;
int numIdle = 0;
progress_.PerformStep();
if (progress_.Value >= progress_.Maximum) {
progress_.Value = progress_.Minimum;
}
foreach (ListViewItem item in list_.Items) {
Site site = (Site) item.Tag;
if (site.status.ToString() != item.SubItems[1].Text) {
item.SubItems[1].Text = site.status.ToString();
}
if (site.status == Bitworking.Site.State.Idle || site.status == Bitworking.Site.State.Working) {
finished = false;
}
if (site.status == Bitworking.Site.State.Working) {
numRunning++;
}
if (site.status == Bitworking.Site.State.Idle) {
numIdle++;
}
}
if (numRunning < news_.MaxThreads && numIdle > 0) {
foreach (ListViewItem item in list_.Items) {
Site site = (Site) item.Tag;
if (site.status == Bitworking.Site.State.Idle) {
site.GoThreaded();
numRunning++;
item.SubItems[1].Text = site.status.ToString();
}
if (news_.MaxThreads == numRunning) {
break;
}
}
}
if (finished) {
try {
news_.SiteMerge += new AggieSiteEvent ( this.MergeEvent ) ;
news_.Merge();
news_.Transform(true);
foreach (ListViewItem item in list_.Items) {
Site site = (Site) item.Tag;
if (site.status.ToString() != item.SubItems[1].Text) {
item.SubItems[1].Text = site.status.ToString();
}
if (site.errorMessage != item.SubItems[3].Text) {
item.SubItems[3].Text = site.errorMessage;
}
}
OpenBrowser() ;
news_.WriteToOPML(); // The MD5 values need to be stored.
}
catch (Exception e) {
MessageBox.Show("There is probably an error in the XSL file: " + e.Message, "Failure transforming the data into Aggie.html", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally {
progress_.Visible = false;
siteListFileName_.Visible = true;
load_.Visible = true;
add_.Enabled = true;
scan_.Enabled = true;
open_.Enabled = File.Exists("Aggie.html");
config_.Enabled = true;
}
} else {
updateTimer_.Enabled = true;
}
}
}
public class _ {
[STAThread]
public static void Main() {
Application.Run(new AggieForm());
}
}
internal class AggieListViewSorter : IComparer {
int columnIdx_ = 0 ;
SortOrder order_ = SortOrder.Ascending ;
internal int ColumnIndex {
get { return columnIdx_ ; }
set {
columnIdx_ = value ;
order_ = SortOrder.Ascending ;
}
}
internal SortOrder Sorting {
get { return order_ ; }
set { order_ = value ; }
}
internal void ToggleDirection() {
order_ = order_ == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending ;
}
public int Compare ( object lhs, object rhs ) {
if ( lhs == null && rhs == null ) throw new ArgumentException() ;
if ( lhs == null ) return -1 ;
if ( rhs == null ) return 1 ;
if ( ! ( lhs is ListViewItem && rhs is ListViewItem ))
throw new ArgumentException() ;
ListViewItem rl = (ListViewItem)(order_ == SortOrder.Ascending ? lhs : rhs ) ;
ListViewItem rr = (ListViewItem)(order_ == SortOrder.Ascending ? rhs : lhs ) ;
return rl.SubItems[columnIdx_].Text.CompareTo(rr.SubItems[columnIdx_].Text) ;
}
}
}
|