/*
* Copyright (C) 2006 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.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using Gtk;
using Mono.Unix;
using DCSharp.Backend.Connections;
using DCSharp.Backend.Managers;
using DCSharp.Backend.Objects;
using DCSharp.Xml;
namespace DCSharp.GUI{
public class Util
{
#region Icon
private static Widget widget = new Invisible();
#if !GNOME
private static string[][] Extensions = {
new string[] {"au", "mp2", "mp3", "mid", "ogg", "rm", "sm", "wav"},
new string[] {"arc", "arj", "gz", "lzh", "pak", "rar", "z", "zip"},
new string[] {"doc", "pdf", "ps", "tex", "txt", "wri"},
new string[] {"bat", "com", "exe", "pm"},
new string[] {"bmp", "gif", "jpg", "jpeg", "pcx", "png", "psd", "wmf"},
new string[] {"avi", "asf", "mov", "mpeg", "mpg"}
};
#endif
private static string[][] icons = {
new string[] {"audio-x-generic", "gnome-mime-audio"},
new string[] {"package-x-generic", "gnome-package"},
new string[] {"text-x-generic", "gtk-file"},
new string[] {"application-x-executable", "gnome-fs-executable"},
new string[] {"image-x-generic", "gnome-mime-image"},
new string[] {"video-x-generic", "gnome-mime-video"},
new string[] {"folder", "gnome-fs-directory"}
};
/*
public enum SearchFileType
{
Any = 1,
Audio,
Package,
Document,
Executable,
Image,
Video,
Directory
}
*/
public static Gdk.Pixbuf GetFileTypeIcon(SearchFileType type)
{
if (type != SearchFileType.Any)
{
return GetIcon(icons[(int)type - 2]);
}
// FIXME: Need another icon for this one
return widget.RenderIcon(Stock.About, IconSize.Menu, null);
}
public static Gdk.Pixbuf GetIconFromPath(string path)
{
return GetIconFromPath(path, 16);
}
public static Gdk.Pixbuf GetIconFromPath(string path, int size)
{
return IconManager.GetIcon(GetIconName(path), size);
}
public static string GetIconName(string fileName)
{
#if GNOME
string mimeType = Gnome.Vfs.Mime.TypeFromName(fileName);
Gnome.IconLookupResultFlags result;
string iconName = Gnome.Icon.Lookup(IconTheme.Default, null, null,
null, null, mimeType, Gnome.IconLookupFlags.None, out result);
// TODO: Replace default icon from Gnome.Icon.Lookup
return iconName;
#else
string extension = System.IO.Path.GetExtension(fileName);
if (extension.Length == 0)
{
return "gtk-file";
}
int index = 0;
extension = extension.ToLower().Substring(1);
foreach (string[] extensions in Extensions)
{
if (Array.IndexOf(extensions, extension) > -1)
{
foreach (string iconName in icons[index])
{
if (IconTheme.Default.HasIcon(iconName))
{
return iconName;
}
}
}
index++;
}
return "gtk-file";
#endif
}
public static Gdk.Pixbuf GetUserIcon(Identity identity)
{
if (identity.User.IsOnline)
{
if (identity.Op)
{
return IconManager.UserOp;
}
else if (identity.Active)
{
return IconManager.UserOnline;
}
return IconManager.UserPassive;
}
return IconManager.UserOffline;
}
public static Gdk.Pixbuf GetUserIcon(User user)
{
return user.IsOnline ? IconManager.UserOnline : IconManager.UserOffline;
}
private static Gdk.Pixbuf GetIcon(string[] iconNames)
{
return GetIcon(iconNames, 16);
}
private static Gdk.Pixbuf GetIcon(string[] iconNames, int size)
{
foreach (string iconName in iconNames)
{
if (IconTheme.Default.HasIcon(iconName))
{
return IconManager.GetIcon(iconName, size);
}
}
return null;
}
#endregion
#region Path
private static char[] PathSeparatorChars = new char [] {
Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar, '\\', '/'
};
public static string GetDirectory(string path)
{
int index = path.LastIndexOfAny(PathSeparatorChars);
if (index > 0)
{
return path.Substring(0, index);
}
return path;
}
public static string GetFile(string path)
{
int index = path.LastIndexOfAny(PathSeparatorChars);
if (index > 0)
{
return path.Substring(index + 1);
}
return path;
}
#endregion
public static string FormatFileSize(long bytes)
{
double size = bytes;
bool decimals = true;
int count = 0;
while (size >= 1024)
{
size /= 1024;
count++;
}
string units = null;
switch(count)
{
case 0:
units = "byte";
decimals = false;
break;
case 1:
units = "KiB";
break;
case 2:
units = "MiB";
break;
case 3:
units = "GiB";
break;
case 4:
units = "TiB";
break;
case 5:
units = "PiB";
break;
case 6:
units = "EiB";
break;
default:
throw new OverflowException();
}
string format = decimals ? "{0:0.0} {1}" : "{0:0} {1}";
return String.Format(format, size, units);
}
#if GNOME
public static void OpenFile(string file)
{
OpenUrl(Gnome.Vfs.Uri.GetUriFromLocalPath(file));
}
public static void OpenUrl(string url)
{
try
{
Gnome.Url.Show(url);
}
catch
{
string text = Catalog.GetString("Could not open \"{0}\"");
text = String.Format(text, Gnome.Vfs.Unescape.StringForDisplay(url));
MessageDialog dialog = new MessageDialog(text, null, null,
MessageType.Error, GUI.MainWindow.Window);
dialog.AddButton(Stock.Ok, ResponseType.Ok);
dialog.Run();
dialog.Destroy();
}
}
#endif
public static string GetStatus(TransferFileInfo transfer)
{
if (transfer is DownloadFileInfo)
{
return GetStatus((DownloadFileInfo)transfer);
}
else if (transfer is UploadFileInfo)
{
return GetStatus((UploadFileInfo)transfer);
}
return null;
}
public static string GetStatus(DownloadFileInfo download)
{
if (download.Active)
{
return Catalog.GetString("Receiving");
}
else if (download.Size > 0 &&
download.Position == download.Size)
{
return Catalog.GetString("Done");
}
else if (!Runtime.DownloadManager.Contains(download))
{
return Catalog.GetString("Aborted");
}
else
{
SourceInfo[] sourceInfos = download.SourceInfos;
int users = sourceInfos.Length;
foreach (SourceInfo source in sourceInfos)
{
if (!source.Available)
{
users--;
}
}
if (users == 0)
{
return Catalog.GetString("No users to download from");
}
int usersOnline = GetOnlineUsers(download);
if (usersOnline > 0)
{
if (users == 1)
{
return Catalog.GetString("Waiting (User online)");
}
return String.Format(
Catalog.GetString("Waiting ({0} of {1} users online)"),
usersOnline, users);
}
else
{
if (users == 1)
{
return Catalog.GetString("User offline");
}
else if (users == 2)
{
return Catalog.GetString("Both users offline");
}
return Catalog.GetString("All users offline");
}
}
}
public static string GetStatus(UploadFileInfo upload)
{
if (upload.Active)
{
return Catalog.GetString("Sending");
}
else if (upload.Size > 0 &&
upload.Position == upload.Size)
{
return Catalog.GetString("Done");
}
else
{
return Catalog.GetString("Aborted");
}
}
public static int GetOnlineUsers(DownloadFileInfo download)
{
int usersOnline = 0;
foreach (SourceInfo source in download.SourceInfos)
{
if (source.Available && source.User.IsOnline)
{
usersOnline++;
}
}
return usersOnline;
}
public static void SetText(Entry entry, string text)
{
entry.Text = text != null ? text : String.Empty;
}
public static string GetHubName(HubConnection connection)
{
lock (Runtime.FavoriteHubManager.SyncRoot)
{
foreach (FavoriteHubInfo hubInfo in Runtime.FavoriteHubManager.Hubs)
{
if (hubInfo.Hostname == connection.Hostname)
{
return hubInfo.Name;
}
}
return connection.Hostname;
}
}
public static void SetVisibleColumns(TreeViewColumn[] columns,
int visibleColumns)
{
int i = 0;
foreach (TreeViewColumn column in columns)
{
column.Visible = (visibleColumns & (1 << i)) > 0;
i++;
}
}
public static int GetVisibleColumns(TreeViewColumn[] columns)
{
int i = 0;
int visible = 0;
foreach (TreeViewColumn column in columns)
{
visible |= column.Visible ? (1 << i) : 0;
i++;
}
return visible;
}
public static void RequestFileList(Identity user)
{
RequestFileList(user, null);
}
public static void RequestFileList(Identity user, string pathToSelect)
{
Debug.Assert(user != null);
DownloadFileInfo download = Runtime.DownloadManager.AddFileList(
user.User);
GUI.ShowFileList(null, user, download, pathToSelect);
}
}
}
|