DownloadView.cs :  » Business-Application » DC » DCSharp » GUI » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Business Application » DC 
DC » DCSharp » GUI » DownloadView.cs
/*
 * 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;

using Gtk;
using Mono.Unix;

using DCSharp.Backend.Connections;
using DCSharp.Backend.Managers;
using DCSharp.Backend.Objects;

namespace DCSharp.GUI{
  public class DownloadView : TransferView
  {
    private ConnectionManager connectionManager;
    private DownloadManager downloadManager;
    
    private ActionGroup actionGroup;
    private TreeViewColumn nameColumn;
    
    public DownloadView(ConnectionManager connectionManager,
      DownloadManager downloadManager) :
      this(connectionManager, downloadManager, null)
    {
    }
    
    public DownloadView(ConnectionManager connectionManager,
      DownloadManager downloadManager, TreeModel model) : base(model)
    {
      if (connectionManager == null)
      {
        throw new ArgumentNullException("connectionManager");
      }
      if (downloadManager == null)
      {
        throw new ArgumentNullException("downloadManager");
      }
      this.connectionManager = connectionManager;
      this.downloadManager = downloadManager;
      
      // User interface
      ActionEntry[] entries = new ActionEntry[] {
        new ActionEntry("Open", Stock.Open, Catalog.GetString("_Open"), null,
          Catalog.GetString("Open"),
          OnOpen),
        new ActionEntry("BrowseTo", null, Catalog.GetString("_Open Folder"), null,
          Catalog.GetString("Open folder containing file"),
          OnBrowseTo),
        new ActionEntry("FindAlternates", null, Catalog.GetString("_Search for Alternates"), null,
          Catalog.GetString("Search for alternates"),
          null),
        new ActionEntry("Remove", Stock.Remove, null, null, null, null)
      };
      
      actionGroup = new ActionGroup("DownloadViewActions");
      actionGroup.Add(entries);
      
      Uim.InsertActionGroup(actionGroup, 0);
      Uim.AddUiFromResource("DownloadView.ui");
      
      actionGroup["Open"].Sensitive = false;
      actionGroup["BrowseTo"].Sensitive = false;
      actionGroup["FindAlternates"].Sensitive = false;
      actionGroup["Remove"].Sensitive = false;
#if !GNOME
      actionGroup["Open"].Visible = false;
      actionGroup["BrowseTo"].Visible = false;
#endif
      // Columns
      TreeViewColumn column;
      CellRendererProgress progressRenderer;
      
      // Name
      nameColumn = GetColumn("Name");
      
      // Progress
      column = GetColumn("Progress");
      progressRenderer = (CellRendererProgress)column.CellRenderers[0];
      
      column.SetCellDataFunc(progressRenderer, SetProgressVisible);
    }
    
    #region Methods
    
    public DownloadFileInfo[] GetSelectedDownloads(bool recurse)
    {
      return base.GetSelectedTransfers<DownloadFileInfo>(recurse);
    }
    
    protected override void OnPopulatePopup(Menu menu)
    {
      bool sensitive = false;
      bool browseSensitive = false;
      
      TreeIter[] iters = GetSelectedIters();
      if (iters.Length == 1)
      {
        DownloadFileInfo download = Model.GetValue(iters[0],
          (int)DownloadStore.Column.Object) as DownloadFileInfo;
        
        if (download != null && download.Size != 0 &&
          download.Position == download.Size)
        {
          sensitive = true;
          browseSensitive = true;
        }
        else
        {
          string path = Model.GetValue(iters[0],
            (int)DownloadStore.Column.Path) as string;
          
          sensitive = path != null;
        }
      }
      
      actionGroup["Open"].Sensitive = sensitive;
      actionGroup["BrowseTo"].Sensitive = browseSensitive;
      
      base.OnPopulatePopup(menu);
    }
    
    protected override void OnRowActivated(TreePath path,
      TreeViewColumn column)
    {
      SourceInfo source;
      DownloadFileInfo download;
      
      TreeIter iter;
      if (Model.GetIter(out iter, path))
      {
#if GNOME
        // It's a download?
        download = Model.GetValue(iter,
          (int)DownloadStore.Column.Object) as DownloadFileInfo;
        
        if (download != null && download.Size != 0 &&
          download.Position == download.Size)
        {
          Util.OpenFile(System.IO.Path.Combine(download.Target, download.Name));
          return;
        }
#endif
        // It's a source?
        if (GetSource(Model, iter, out source, out download) &&
          !source.Active && source.Available && source.User.IsOnline)
        {
          downloadManager.SetNextDownload(source.User, download, true);
          return;
        }
#if GNOME
        else
        {
          // It's a directory?
          string p = Model.GetValue(iter, (int)DownloadStore.Column.Path) as string;
          if (p != null)
          {
            Util.OpenFile(p);
          }
        }
#endif
      }
      base.OnRowActivated(path, column);
    }
    
    #region CellDataFuncs
    
    protected override void SetPixbuf(TreeViewColumn column,
      CellRendererPixbuf pixbufRenderer, TreeModel model, TreeIter iter,
      object obj)
    {
      SourceInfo source = obj as SourceInfo;
      if (source != null)
      {
        if (source.Active)
        {
          pixbufRenderer.Pixbuf = null;
          pixbufRenderer.StockId = Stock.GoDown;
        }
        else
        {
          pixbufRenderer.Pixbuf = source.User.IsOnline ?
            IconManager.UserOnline : IconManager.UserOffline;
        }
      }
      else if (obj is DownloadFileInfo)
      {
        base.SetPixbuf(column, pixbufRenderer, model, iter, obj);
      }
      else
      {
        string icon = Stock.Directory;
        string path = model.GetValue(iter,
          (int)DownloadStore.Column.Path) as string;
        
        if (path != null)
        {
          if (path == System.IO.Path.GetPathRoot(path))
          {
            icon = Stock.Harddisk;
          }
          else if (path == Environment.GetFolderPath(Environment.SpecialFolder.Personal))
          {
            pixbufRenderer.Pixbuf = IconManager.GetIcon("user-home", 16);
            return;
          }
        }
        pixbufRenderer.Pixbuf = RenderIcon(icon, IconSize.Menu, null);
      }
    }
    
    protected override void SetStatusText(TreeViewColumn column,
      CellRendererText textRenderer, TreeModel model, TreeIter iter,
      object obj)
    {
      SourceInfo source = obj as SourceInfo;
      if (source != null)
      {
        string text = null;
        if (!source.Available)
        {
          text = Catalog.GetString("File not available");
        }
        else if (connectionManager.IsRequestingConnectionTo(source.User))
        {
          text = Catalog.GetString("Connecting...");
        }
        else if (connectionManager.UserIsBusy(source.User))
        {
          text = Catalog.GetString("No slots available");
        }
        textRenderer.Text = text; 
        
        SetTextStyle(column, textRenderer, model, iter, obj);
      }
      else
      {
        base.SetStatusText(column, textRenderer, model, iter, obj);
      }
    }
    
    protected override void SetTextStyle(TreeViewColumn column,
      CellRendererText textRenderer, TreeModel model, TreeIter iter,
      object obj)
    {
      if (column == nameColumn)
      {
        DownloadFileInfo download = obj as DownloadFileInfo;
        if (download != null && download.Size != download.Position &&
          !downloadManager.Contains(download))
        {
          // Download aborted
          textRenderer.Strikethrough = true;
        }
        else
        {
          textRenderer.Strikethrough = false;
        }
      }
      
      SourceInfo source = obj as SourceInfo;
      if (source != null)
      {
        if (!source.Available)
        {
          textRenderer.Foreground = "dark red";
        }
        else if (!source.Active)
        {
          textRenderer.Foreground = "dim grey";
        }
        else
        {
          textRenderer.Foreground = "black";
        }
      }
      else
      {
        base.SetTextStyle(column, textRenderer, model, iter, obj);
      }
    }
    
    private void SetProgressVisible(TreeViewColumn column,
      CellRenderer renderer, TreeModel model, TreeIter iter)
    {
      CellRendererProgress progressRenderer = renderer as CellRendererProgress;
      if (progressRenderer != null)
      {
        progressRenderer.Visible = !(model.GetValue(iter, (int)DownloadStore.Column.Object)
          is SourceInfo);
      }
    }
    
    #endregion
    
    private bool GetSource(TreeModel model, TreeIter iter,
      out SourceInfo source, out DownloadFileInfo download)
    {
      source = model.GetValue(iter, (int)DownloadStore.Column.Object)
        as SourceInfo;
      
      if (source != null)
      {
        TreeIter parent;
        if (model.IterParent(out parent, iter))
        {
          download = model.GetValue(parent,
            (int)DownloadStore.Column.Object) as DownloadFileInfo;
          
          if (download != null)
          {
            return true;
          }
        }
      }
      source = null;
      download = null;
      return false;
    }
    
    private void OnOpen(object obj, EventArgs args)
    {
#if GNOME
      TreeIter[] iters = GetSelectedIters();
      if (iters.Length == 1)
      {
        DownloadFileInfo download = Model.GetValue(iters[0],
          (int)DownloadStore.Column.Object) as DownloadFileInfo;
        
        if (download != null && download.Size != 0 &&
          download.Position == download.Size)
        {
          Util.OpenFile(System.IO.Path.Combine(download.Target, download.Name));
        }
        else
        {
          string path = Model.GetValue(iters[0],
            (int)DownloadStore.Column.Path) as string;
          if (path != null)
          {
            Util.OpenFile(path);
          }
        }
      }
#endif
    }
    
    private void OnBrowseTo(object obj, EventArgs args)
    {
#if GNOME
      TreeIter[] iters = GetSelectedIters();
      if (iters.Length == 1)
      {
        DownloadFileInfo download = Model.GetValue(iters[0],
          (int)DownloadStore.Column.Object) as DownloadFileInfo;
        
        if (download != null && download.Size != 0 &&
          download.Position == download.Size)
        {
          Util.OpenFile(download.Target);
        }
      }
#endif
    }
    
    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.