/*
* 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 Mono.Unix;
using DCSharp.Backend.Managers;
using DCSharp.Backend.Objects;
using DCSharp.Xml;
using DCSharp.Xml.FileList;
namespace DCSharp.GUI{
public class FileListingView : ExtendedTreeView
{
private ActionGroup actionGroup;
#region Constructors
public FileListingView(TreeModel model, Identity user) : base(model)
{
this.user = user;
CanActivateMultipleRows = true;
// User interface
ActionEntry[] entries = new ActionEntry[] {
new ActionEntry("Download", null, Catalog.GetString("_Download"), null,
Catalog.GetString("Save file"),
OnDownload),
new ActionEntry("SaveTo", Stock.SaveAs, Catalog.GetString("_Save To..."), null,
Catalog.GetString("Save file to a different directory"),
OnSaveAs)
};
actionGroup = new ActionGroup("FileListingViewActions");
actionGroup.Add(entries);
Uim.InsertActionGroup(actionGroup, 0);
Uim.AddUiFromResource("FileListingView.ui");
// Columns
TreeViewColumn column;
CellRendererText textRenderer;
CellRendererPixbuf pixbufRenderer;
// Name
column = new TreeViewColumn();
column.Title = Catalog.GetString("Name");
column.SortColumnId = (int)FileListingStore.Column.Name;
column.Resizable = true;
column.Spacing = 3;
AppendColumn(column);
pixbufRenderer = new CellRendererPixbuf();
column.PackStart(pixbufRenderer , false);
column.SetCellDataFunc(pixbufRenderer, SetPixbuf);
textRenderer = new CellRendererText();
column.PackStart(textRenderer , true);
column.AddAttribute(textRenderer, "text", (int)FileListingStore.Column.Name);
// Size
textRenderer = new CellRendererText();
column = AppendColumn(Catalog.GetString("Size"), textRenderer,
"text", (int)FileListingStore.Column.Size);
column.Resizable = true;
column.SortColumnId = (int)FileListingStore.Column.Bytes;
// TTH
textRenderer = new CellRendererText();
textRenderer.Ellipsize = Pango.EllipsizeMode.Middle;
column = AppendColumn(Catalog.GetString("TTH"), textRenderer,
"text", (int)FileListingStore.Column.TTH);
column.Expand = true;
column.Resizable = true;
column.SortColumnId = (int)FileListingStore.Column.TTH;
}
#endregion
#region Properties
public Identity User
{
get { return user; }
}
private Identity user;
#endregion
#region Methods
public TreePath ExpandToPathString(string path)
{
if (path.StartsWith(new string(System.IO.Path.DirectorySeparatorChar, 1)))
{
path = path.Substring(1);
}
TreeIter iter;
TreePath treePath = null;
if (!Model.GetIterFirst(out iter))
{
return null;
}
int i = 0;
string[] dirs = path.Split(System.IO.Path.DirectorySeparatorChar);
bool hasIter = false;
do
{
string name = Model.GetValue(iter,
(int)FileListingStore.Column.Name) as string;
if (name == dirs[i])
{
treePath = Model.GetPath(iter);
// Load any child iters
ExpandToPath(treePath);
hasIter = Model.IterChildren(out iter, iter);
if (hasIter)
{
i++;
continue;
}
break;
}
hasIter = Model.IterNext(ref iter);
}
while (hasIter && i < dirs.Length);
return treePath;
}
protected override void OnRowActivated(TreePath path,
TreeViewColumn column)
{
TreeIter iter;
if (!Model.GetIter(out iter, path))
{
return;
}
File file = Model.GetValue(iter,
(int)FileListingStore.Column.Object) as File;
if (file != null)
{
string filePath = System.IO.Path.Combine(BuildPath(Model, iter),
file.Name);
Download(file, filePath, Runtime.Settings.DownloadDirectory);
return;
}
base.OnRowActivated(path, column);
}
#region Downloading
private void Download(File file, string path, string target)
{
Download(file, path, target, file.Name);
}
private void Download(File file, string path, string target, string name)
{
SourceInfo source = new SourceInfo(user, path);
DownloadFileInfo download = null;
if (file.TTH != null)
{
download = Runtime.DownloadManager.GetDownload(file.TTH);
}
if (download == null)
{
download = new DownloadFileInfo(name, file.Size, file.TTH, target);
download.TempTarget = download.Target;
download.Add(source);
Runtime.DownloadManager.Add(download);
}
else if (!download.HasSource(source))
{
download.Add(source);
}
}
private void Download(Directory directory, string path, string target)
{
Download(directory, path, target, directory.Name);
}
private void Download(Directory directory, string path, string target,
string name)
{
target = System.IO.Path.Combine(target, name);
foreach (Directory childDir in directory.Directories)
{
Download(childDir, System.IO.Path.Combine(path, childDir.Name),
target);
}
foreach (File file in directory.Files)
{
string filePath = System.IO.Path.Combine(path, file.Name);
Download(file, filePath, target);
}
}
private string BuildPath(TreeModel model, TreeIter iter)
{
TreeIter parent;
if (model.IterParent(out parent, iter))
{
Directory directory = model.GetValue(parent,
(int)FileListingStore.Column.Object) as Directory;
if (directory != null)
{
return System.IO.Path.Combine(BuildPath(model, parent),
directory.Name);
}
}
return String.Empty;
}
#endregion
private void SetPixbuf(TreeViewColumn column, CellRenderer renderer,
TreeModel model, TreeIter iter)
{
CellRendererPixbuf pixbufRenderer = renderer as CellRendererPixbuf;
if (pixbufRenderer != null)
{
object file = model.GetValue(iter, (int)FileListingStore.Column.Object);
if (file is Directory)
{
pixbufRenderer.Pixbuf = RenderIcon(Stock.Directory,
IconSize.Menu, String.Empty);
}
else if (file is File)
{
pixbufRenderer.Pixbuf = Util.GetIconFromPath((file as File).Name);
}
}
}
private void OnDownload(object obj, EventArgs args)
{
TreeIter[] iters = GetSelectedIters();
foreach (TreeIter iter in iters)
{
Download(iter, Runtime.Settings.DownloadDirectory);
}
}
private void OnSaveAs(object obj, EventArgs args)
{
TreeIter[] iters = GetSelectedIters();
if (iters.Length == 1)
{
string name = Model.GetValue(iters[0],
(int)FileListingStore.Column.Name) as string;
string filename = SaveToDialog.GetFilename(name, Toplevel as Window);
if (filename != null)
{
Download(iters[0], System.IO.Path.GetDirectoryName(filename),
System.IO.Path.GetFileName(filename));
}
}
else
{
string folder = SaveToDialog.GetFolder(Toplevel as Window);
if (folder != null)
{
foreach (TreeIter iter in iters)
{
Download(iter, folder);
}
}
}
}
private void Download(TreeIter iter, string target)
{
Download(iter, target, null);
}
private void Download(TreeIter iter, string target, string name)
{
Directory directory = Model.GetValue(iter,
(int)FileListingStore.Column.Object) as Directory;
if (directory != null)
{
string path = System.IO.Path.Combine(BuildPath(Model, iter),
directory.Name);
if (name != null)
{
Download(directory, path, target, name);
}
else
{
Download(directory, path, target);
}
}
File file = Model.GetValue(iter,
(int)FileListingStore.Column.Object) as File;
if (file != null)
{
string path = System.IO.Path.Combine(BuildPath(Model, iter),
file.Name);
if (name != null)
{
Download(file, path, target, name);
}
else
{
Download(file, path, target);
}
}
}
#endregion
}
}
|