/*
* 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;
namespace DCSharp.GUI{
public class SearchResultView : ExtendedTreeView
{
public event EventHandler SearchTTH;
private ActionGroup actionGroup;
#region Constructors
public SearchResultView() : this(null)
{
}
public SearchResultView(TreeModel model) : base(model)
{
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),
new ActionEntry("FileList", Stock.Directory, Catalog.GetString("_Browse Files"), null,
Catalog.GetString("Browse the users files"),
OnFileList),
new ActionEntry("SearchTTH", Stock.Find, Catalog.GetString("_Find Similar Files"), null,
Catalog.GetString("Find similar files"),
OnSearchTTH)
};
actionGroup = new ActionGroup("SearchResultViewActions");
actionGroup.Add(entries);
Uim.InsertActionGroup(actionGroup, 0);
Uim.AddUiFromResource("SearchResultView.ui");
actionGroup["SearchTTH"].Sensitive = false;
// Columns
TreeViewColumn column;
CellRendererText textRenderer;
CellRendererPixbuf pixbufRenderer;
// Name
column = new TreeViewColumn();
column.Title = Catalog.GetString("Name");
column.SortColumnId = (int)SearchResultStore.Column.Name;
column.Expand = true;
column.Resizable = true;
column.Spacing = 3;
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)SearchResultStore.Column.Name);
// Directory
textRenderer = new CellRendererText();
textRenderer.Ellipsize = Pango.EllipsizeMode.Middle;
column = AppendColumn(Catalog.GetString("Directory"), textRenderer,
"text", (int)SearchResultStore.Column.Directory);
column.Expand = true;
column.Resizable = true;
column.SortColumnId = (int)SearchResultStore.Column.Directory;
// Size
textRenderer = new CellRendererText();
column = AppendColumn(Catalog.GetString("Size"), textRenderer,
"text", (int)SearchResultStore.Column.Size);
column.SortColumnId = (int)SearchResultStore.Column.Bytes;
// User
textRenderer = new CellRendererText();
textRenderer.Ellipsize = Pango.EllipsizeMode.End;
column = AppendColumn(Catalog.GetString("User"), textRenderer,
"text", (int)SearchResultStore.Column.User);
column.Expand = true;
column.Resizable = true;
column.SortColumnId = (int)SearchResultStore.Column.User;
// Slots
textRenderer = new CellRendererText();
column = AppendColumn(Catalog.GetString("Slots"), textRenderer,
"text", (int)SearchResultStore.Column.Slots);
column.SortColumnId = (int)SearchResultStore.Column.FreeSlots;
// Connection
textRenderer = new CellRendererText();
column = AppendColumn(Catalog.GetString("Connection"), textRenderer, "text",
(int)SearchResultStore.Column.Connection);
column.SortColumnId = (int)SearchResultStore.Column.Connection;
// TTH
textRenderer = new CellRendererText();
textRenderer.Ellipsize = Pango.EllipsizeMode.Middle;
column = AppendColumn(Catalog.GetString("TTH"), textRenderer,
"text", (int)SearchResultStore.Column.TTH);
column.Expand = true;
column.Resizable = true;
column.SortColumnId = (int)SearchResultStore.Column.TTH;
}
#endregion
#region Methods
protected override void OnRowActivated(TreePath path,
TreeViewColumn column)
{
TreeIter iter;
if (Model.GetIter(out iter, path))
{
SearchResult result = Model.GetValue(iter,
(int)SearchResultStore.Column.Object) as SearchResult;
if (result != null)
{
Download(result);
return;
}
}
base.OnRowActivated(path, column);
}
protected override void OnPopulatePopup(Menu menu)
{
bool canDownload = false;
object[] results = GetSelectedObjects((int)SearchResultStore.Column.Object);
foreach (SearchResult result in results)
{
if (result.Type == ResultType.File)
{
canDownload = true;
}
}
actionGroup["Download"].Sensitive = canDownload;
base.OnPopulatePopup(menu);
}
private void Download(SearchResult result)
{
Download(result, Util.GetFile(result.Path),
Runtime.Settings.DownloadDirectory);
}
private void Download(SearchResult result, string name, string target)
{
if (result != null && result.Type == ResultType.File)
{
SourceInfo source = new SourceInfo(result.User, result.Path);
DownloadFileInfo download = null;
if (result.TTH != null)
{
download = Runtime.DownloadManager.GetDownload(result.TTH);
}
if (download == null)
{
download = new DownloadFileInfo(name, result.Size, result.TTH, target);
download.TempTarget = download.Target;
download.Add(source);
Runtime.DownloadManager.Add(download);
}
else if (!download.HasSource(source))
{
download.Add(source);
}
}
}
private void SetPixbuf(TreeViewColumn column, CellRenderer renderer,
TreeModel model, TreeIter iter)
{
CellRendererPixbuf pixbufRenderer = renderer as CellRendererPixbuf;
SearchResult result = model.GetValue(iter,
(int)SearchResultStore.Column.Object) as SearchResult;
if (result != null && pixbufRenderer != null)
{
if (result.Type == ResultType.Directory)
{
pixbufRenderer.Pixbuf = RenderIcon(Stock.Directory,
IconSize.Menu, String.Empty);
}
else
{
pixbufRenderer.Pixbuf = Util.GetIconFromPath(result.Path);
}
}
}
private void OnDownload(object obj, EventArgs args)
{
TreePath[] paths = Selection.GetSelectedRows();
foreach (TreePath path in paths)
{
ActivateRow(path, Columns[0]);
}
}
private void OnSaveAs(object obj, EventArgs args)
{
object[] results = GetSelectedObjects((int)SearchResultStore.Column.Object);
if (results.Length == 1)
{
SearchResult result = results[0] as SearchResult;
string filename = SaveToDialog.GetFilename(Util.GetFile(result.Path),
Toplevel as Window);
if (filename != null)
{
Download(result, System.IO.Path.GetFileName(filename),
System.IO.Path.GetDirectoryName(filename));
}
}
else
{
string folder = SaveToDialog.GetFolder(Toplevel as Window);
if (folder != null)
{
foreach (SearchResult result in results)
{
Download(result, Util.GetFile(result.Path), folder);
}
}
}
}
private void OnFileList(object obj, EventArgs args)
{
object[] results = GetSelectedObjects((int)SearchResultStore.Column.Object);
foreach (SearchResult result in results)
{
Util.RequestFileList(result.User, result.Path);
}
}
private void OnSearchTTH(object obj, EventArgs args)
{
if (SearchTTH != null)
{
SearchTTH(this, EventArgs.Empty);
}
}
#endregion
}
}
|