/*
* 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 System.Collections.Generic;
using Gtk;
using Mono.Unix;
using DCSharp.Backend.Objects;
namespace DCSharp.GUI{
// NOTE: Any model used with this class must contains the following
// columns.
public enum TransferStoreColumn
{
Object,
Name,
Directory,
SizeText,
Size,
Position,
ProgressText,
Progress,
TimeLeft
};
public class TransferView : ExtendedTreeView
{
public TransferView(TreeModel model) : base(model)
{
// Columns
TreeViewColumn column;
CellRendererPixbuf pixbufRenderer;
CellRendererText textRenderer;
CellRendererProgress progressRenderer;
// Name
column = new TreeViewColumn();
column.Title = Catalog.GetString("Name");
column.Expand = true;
column.Resizable = true;
column.SortColumnId = (int)TransferStoreColumn.Name;
AppendColumn(column);
pixbufRenderer = new CellRendererPixbuf();
column.PackStart(pixbufRenderer, false);
column.SetCellDataFunc(pixbufRenderer, SetPixbuf);
textRenderer = new CellRendererText();
textRenderer.Ellipsize = Pango.EllipsizeMode.End;
column.PackStart(textRenderer, true);
column.AddAttribute(textRenderer, "text",
(int)TransferStoreColumn.Name);
column.SetCellDataFunc(textRenderer, SetTextStyle);
ExpanderColumn = column;
// Size
textRenderer = new CellRendererText();
column = AppendColumn(Catalog.GetString("Size"), textRenderer,
"text", (int)TransferStoreColumn.SizeText);
column.SortColumnId = (int)TransferStoreColumn.Size;
column.SetCellDataFunc(textRenderer, SetTextStyle);
// Status
textRenderer = new CellRendererText();
//textRenderer.Ellipsize = Pango.EllipsizeMode.End;
column = AppendColumn(Catalog.GetString("Status"), textRenderer);
column.Resizable = true;
column.SetCellDataFunc(textRenderer, SetStatusText);
// Progress
progressRenderer = new CellRendererProgress();
column = AppendColumn(Catalog.GetString("Progress"), progressRenderer);
column.Resizable = true;
column.AddAttribute(progressRenderer, "text",
(int)TransferStoreColumn.ProgressText);
column.AddAttribute(progressRenderer, "value",
(int)TransferStoreColumn.Progress);
column.SortColumnId = (int)TransferStoreColumn.Progress;
// Time left
textRenderer = new CellRendererText();
column = AppendColumn(Catalog.GetString("Time Left"), textRenderer,
"text", (int)TransferStoreColumn.TimeLeft);
column.SortColumnId = (int)TransferStoreColumn.TimeLeft;
column.SetCellDataFunc(textRenderer, SetTextStyle);
}
#region Methods
public T[] GetSelectedTransfers<T>(bool recurse) where T : TransferFileInfo
{
TreeIter[] selectedIters = GetSelectedIters();
List<T> transfers = new List<T>();
foreach (TreeIter iter in selectedIters)
{
AddTransfersToList(transfers, iter, recurse);
}
return transfers.ToArray();
}
private void AddTransfersToList<T>(List<T> transfers, TreeIter iter,
bool recurse) where T : TransferFileInfo
{
TreeIter childIter;
T transfer = Model.GetValue(iter, (int)TransferStoreColumn.Object) as T;
if (transfer != null)
{
if (!transfers.Contains(transfer))
{
transfers.Add(transfer);
}
}
else if (recurse && Model.IterChildren(out childIter, iter))
{
do
{
AddTransfersToList(transfers, childIter, true);
}
while (Model.IterNext(ref childIter));
}
}
#region CellDataFuncs
protected virtual void SetPixbuf(TreeViewColumn column,
CellRendererPixbuf pixbufRenderer, TreeModel model, TreeIter iter,
object obj)
{
TransferFileInfo transfer = obj as TransferFileInfo;
if (transfer != null)
{
if (transfer.Name != null)
{
pixbufRenderer.Pixbuf = Util.GetIconFromPath(transfer.Name);
}
else
{
pixbufRenderer.Pixbuf = null;
}
}
}
protected virtual void SetStatusText(TreeViewColumn column,
CellRendererText textRenderer, TreeModel model, TreeIter iter,
object obj)
{
TransferFileInfo transfer = obj as TransferFileInfo;
textRenderer.Text = Util.GetStatus(transfer);
SetTextStyle(column, textRenderer, model, iter, obj);
}
protected virtual void SetTextStyle(TreeViewColumn column,
CellRendererText textRenderer, TreeModel model, TreeIter iter,
object obj)
{
TransferFileInfo transfer = obj as TransferFileInfo;
if (transfer != null && !transfer.Active &&
transfer.Position < transfer.Size)
{
textRenderer.Foreground = "dim grey";
}
else
{
textRenderer.Foreground = "black";
}
}
private void SetPixbuf(TreeViewColumn column, CellRenderer renderer,
TreeModel model, TreeIter iter)
{
object obj = model.GetValue(iter, (int)TransferStoreColumn.Object);
SetPixbuf(column, (CellRendererPixbuf)renderer, model, iter, obj);
}
private void SetStatusText(TreeViewColumn column, CellRenderer renderer,
TreeModel model, TreeIter iter)
{
object obj = model.GetValue(iter, (int)TransferStoreColumn.Object);
SetStatusText(column, (CellRendererText)renderer, model, iter, obj);
}
private void SetTextStyle(TreeViewColumn column, CellRenderer renderer,
TreeModel model, TreeIter iter)
{
object obj = model.GetValue(iter, (int)TransferStoreColumn.Object);
SetTextStyle(column, (CellRendererText)renderer, model, iter, obj);
}
#endregion
#endregion
}
}
|