/*
* Copyright (C) 2006-2007 Eskil Bylund
*
* 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 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using Gtk;
using Glade;
using Mono.Unix;
using DCSharp.Backend.Connections;
using DCSharp.Backend.Managers;
using DCSharp.Backend.Objects;
using DCSharp.Xml.FileList;
namespace DCSharp.GUI{
public class FileListingPage : Page
{
private DownloadManager downloadManager;
private FileListingStore fileStore;
private FileListingView fileView;
private uint timeout;
private VisibleColumnsWindow columnsWindow;
private string pathToSelect;
[Widget]
private Box statusBox;
[Widget]
private Label statusLabel;
[Widget]
private ProgressBar progressBar;
[Widget]
private ScrolledWindow scrolledWindow;
#region Constructors
public FileListingPage(FileListing fileListing, Identity user,
DownloadFileInfo fileListDownload, string pathToSelect) : base("FileListingPage.glade")
{
this.downloadManager = Runtime.DownloadManager;
this.pathToSelect = pathToSelect;
// User interface
ActionEntry[] entries = new ActionEntry[] {
new ActionEntry("VisibleColumns", null, Catalog.GetString("_Visible Columns..."), null,
Catalog.GetString("Edit visible columns"),
OnVisibleColumns),
};
actionGroup = new ActionGroup("FileListingPageActions");
actionGroup.Add(entries);
// Model
fileStore = new FileListingStore(fileListing);
fileStore.SetSortFunc((int)FileListingStore.Column.Name, FileSortFunc);
fileStore.SetSortColumnId((int)FileListingStore.Column.Name,
SortType.Ascending);
// View
fileView = new FileListingView(fileStore, user);
fileView.RowExpanded += OnRowExpanded;
fileView.Show();
fileView.GetColumn("TTH").Visible = false;
scrolledWindow.Add(fileView);
FileListing = fileListing;
FileListDownload = fileListDownload;
}
#endregion
#region Properties
public override string Title
{
get { return fileView.User.Nick; }
}
public override Gdk.Pixbuf Icon
{
get { return this.RenderIcon(Stock.Directory, IconSize.Menu, null); }
}
public override string Tooltip
{
get { return null; }
}
public override ActionGroup Actions
{
get { return actionGroup; }
}
private ActionGroup actionGroup;
public int VisibleColumns
{
get { return Util.GetVisibleColumns(fileView.Columns); }
set { Util.SetVisibleColumns(fileView.Columns, value | 1); }
}
public FileListing FileListing
{
get { return fileStore.FileListing; }
set
{
fileStore.FileListing = value;
if (value != null && pathToSelect != null)
{
SelectPath(pathToSelect);
pathToSelect = null;
}
UpdateStatus();
OnPageChanged();
}
}
public Identity User
{
get { return fileView.User; }
}
public DownloadFileInfo FileListDownload
{
get { return fileListDownload; }
set
{
if (fileListDownload != null)
{
fileListDownload.ActiveChanged -= OnActiveChanged;
Runtime.Listener.UserAdded -= OnUserEvent;
Runtime.Listener.UserRemoved -= OnUserEvent;
}
fileListDownload = value;
if (value != null)
{
fileListDownload.ActiveChanged += OnActiveChanged;
Runtime.Listener.UserAdded += OnUserEvent;
Runtime.Listener.UserRemoved += OnUserEvent;
}
UpdateStatus();
OnPageChanged();
}
}
private DownloadFileInfo fileListDownload;
#endregion
#region Methods
public override void Dispose()
{
if (fileListDownload != null)
{
downloadManager.Remove(fileListDownload);
FileListDownload = null;
}
if (FileListing != null)
{
fileStore.FileListing = null;
}
if (timeout != 0)
{
GLib.Source.Remove(timeout);
timeout = 0;
}
if (columnsWindow != null)
{
columnsWindow.Destroy();
columnsWindow = null;
}
base.Dispose();
GC.SuppressFinalize(this);
}
public void SelectPath(string path)
{
if (path == null)
{
throw new ArgumentNullException("path");
}
path = path.Replace('\\', System.IO.Path.DirectorySeparatorChar);
path = path.Replace(System.IO.Path.AltDirectorySeparatorChar,
System.IO.Path.DirectorySeparatorChar);
if (FileListing != null)
{
TreePath treePath = fileView.ExpandToPathString(path);
if (treePath != null)
{
fileView.Selection.SelectPath(treePath);
fileView.ScrollToCell(treePath, null, true, 0.5F, 0);
}
}
}
private void UpdateStatus()
{
bool removeTimeout = false;
if (FileListing != null)
{
statusBox.Visible = false;
scrolledWindow.Visible = true;
Status = Catalog.GetString("File list downloaded");
removeTimeout = true;
}
else
{
statusBox.Visible = true;
scrolledWindow.Visible = false;
if (fileListDownload != null)
{
Status = Util.GetStatus(fileListDownload);
if (Util.GetOnlineUsers(fileListDownload) > 0)
{
if (timeout == 0)
{
timeout = GLib.Timeout.Add(10, OnProgressTimeout);
}
}
else
{
removeTimeout = true;
}
}
else
{
Status = Catalog.GetString("Waiting...");
}
}
statusLabel.Markup = "<span size=\"large\" weight=\"bold\">" +
Status + "</span>";
if (removeTimeout && timeout != 0)
{
GLib.Source.Remove(timeout);
timeout = 0;
progressBar.Fraction = 0;
}
}
private int FileSortFunc(TreeModel model, TreeIter tia, TreeIter tib)
{
object obja = model.GetValue(tia, (int)FileListingStore.Column.Object);
object objb = model.GetValue(tib, (int)FileListingStore.Column.Object);
if (obja is Directory && objb is File)
{
return -1;
}
if (obja is File && objb is Directory)
{
return 1;
}
string namea = model.GetValue(tia, (int)FileListingStore.Column.Name) as string;
string nameb = model.GetValue(tib, (int)FileListingStore.Column.Name) as string;
return String.Compare(namea, nameb);
}
private void OnVisibleColumns(object obj, EventArgs args)
{
if (columnsWindow == null)
{
columnsWindow = new VisibleColumnsWindow(fileView.Columns,
Toplevel as Window, fileView.Columns[0]);
columnsWindow.Window.Destroyed += delegate
{
columnsWindow = null;
};
}
columnsWindow.Show();
}
private bool OnProgressTimeout()
{
if (fileListDownload != null && fileListDownload.Active)
{
if (fileListDownload.Size > 0 &&
fileListDownload.Position <= fileListDownload.Size)
{
progressBar.Fraction = (double)fileListDownload.Position /
fileListDownload.Size;
}
string text = String.Format(
Catalog.GetString("{0} of {1} downloaded, about {2} left"),
Util.FormatFileSize(fileListDownload.Position),
Util.FormatFileSize(fileListDownload.Size),
fileListDownload.GetRemainingTime());
progressBar.Text = text;
}
else
{
progressBar.Pulse();
progressBar.Text = String.Empty;
}
return true;
}
private void OnRowExpanded(object obj, RowExpandedArgs args)
{
TreeIter iter;
if (fileStore.GetIter(out iter, args.Path))
{
fileStore.Load(iter);
}
}
private void OnActiveChanged(object obj, EventArgs args)
{
Application.Invoke(delegate
{
UpdateStatus();
});
}
private void OnUserEvent(object obj, UserEventArgs args)
{
if (args.User.Cid == fileListDownload.Name)
{
Application.Invoke(delegate
{
UpdateStatus();
});
}
}
#endregion
}
}
|