/*
* 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.IO;
using System.Collections.Generic;
using Gtk;
using Mono.Unix;
using DCSharp.Backend.Objects;
namespace DCSharp.GUI{
public class UploadStore : ListStore
{
private Dictionary<UploadFileInfo, TreeIter> uploadToIter;
public enum Column {
Object,
Name,
Path,
SizeText,
Size,
Position,
ProgressText,
Progress,
TimeLeft,
User,
Started
};
public UploadStore() : base(typeof(object), typeof(string),
typeof(string), typeof(string), typeof(double), typeof(double),
typeof(string), typeof(int), typeof(string), typeof(string),
typeof(string))
{
uploadToIter = new Dictionary<UploadFileInfo, TreeIter>();
}
#region Methods
public TreeIter Add(UploadFileInfo upload)
{
if (upload == null)
{
throw new ArgumentNullException("upload");
}
TreeIter iter = AppendValues(upload);
SetValue(iter, (int)Column.Started, DateTime.Now.ToString());
uploadToIter.Add(upload, iter);
Update(upload);
return iter;
}
public TreeIter Remove(UploadFileInfo upload)
{
if (upload == null)
{
throw new ArgumentNullException("upload");
}
TreeIter iter = uploadToIter[upload];
uploadToIter.Remove(upload);
Remove(ref iter);
return iter;
}
public void Update(UploadFileInfo upload)
{
if (upload == null)
{
throw new ArgumentNullException("upload");
}
if (!uploadToIter.ContainsKey(upload))
{
return;
}
TreeIter iter = uploadToIter[upload];
if (!upload.IsFileList)
{
SetValue(iter, (int)Column.Name, Path.GetFileName(upload.Name));
SetValue(iter, (int)Column.Path, Path.GetDirectoryName(upload.Name));
}
else
{
SetValue(iter, (int)Column.Name, Catalog.GetString("File list"));
}
SetValue(iter, (int)Column.Size, (double)upload.Size);
SetValue(iter, (int)Column.SizeText, Util.FormatFileSize(upload.Size));
SetValue(iter, (int)Column.User, upload.User.Nick);
// Progress
string progressText = String.Format(Catalog.GetString("{0} of {1}"),
Util.FormatFileSize(upload.Position),
Util.FormatFileSize(upload.Size));
double progress = (double)upload.Position / upload.Size;
SetValue(iter, (int)Column.ProgressText, " " + progressText + " ");
SetValue(iter, (int)Column.Progress, (int)(progress * 100));
// Remaining
if (upload.Active && upload.Bps > 0)
{
TimeSpan remaining = upload.GetRemainingTime();
SetValue(iter, (int)Column.TimeLeft, remaining.ToString());
}
else
{
SetValue(iter, (int)Column.TimeLeft, null);
}
}
public bool Contains(UploadFileInfo upload)
{
return uploadToIter.ContainsKey(upload);
}
#endregion
}
}
|