2006-12-08 19:06:49 +00:00
|
|
|
# dcommon.py
|
2006-11-28 22:28:37 +00:00
|
|
|
#
|
2006-12-08 19:06:49 +00:00
|
|
|
# Copyright (C) Zach Tibbitts 2006 <zach@collegegeek.org>
|
2007-01-08 19:38:19 +00:00
|
|
|
#
|
|
|
|
# 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, or (at your option)
|
2006-12-08 19:06:49 +00:00
|
|
|
# any later version.
|
|
|
|
#
|
2007-01-08 19:38:19 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
2006-12-08 19:06:49 +00:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2007-01-08 19:38:19 +00:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2006-12-08 19:06:49 +00:00
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2007-01-08 19:38:19 +00:00
|
|
|
# along with this program. If not, write to:
|
2007-07-11 04:22:44 +00:00
|
|
|
# The Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor
|
|
|
|
# Boston, MA 02110-1301, USA.
|
2007-06-12 16:33:32 +00:00
|
|
|
#
|
|
|
|
# In addition, as a special exception, the copyright holders give
|
|
|
|
# permission to link the code of portions of this program with the OpenSSL
|
|
|
|
# library.
|
|
|
|
# You must obey the GNU General Public License in all respects for all of
|
|
|
|
# the code used other than OpenSSL. If you modify file(s) with this
|
|
|
|
# exception, you may extend this exception to your version of the file(s),
|
|
|
|
# but you are not obligated to do so. If you do not wish to do so, delete
|
|
|
|
# this exception statement from your version. If you delete this exception
|
|
|
|
# statement from all source files in the program, then also delete it here.
|
2006-11-28 22:28:37 +00:00
|
|
|
|
2007-07-11 04:22:44 +00:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import webbrowser
|
|
|
|
import xdg
|
|
|
|
import xdg.BaseDirectory
|
2006-11-28 22:28:37 +00:00
|
|
|
|
2007-02-16 04:30:49 +00:00
|
|
|
import gettext
|
|
|
|
|
2006-12-04 23:59:45 +00:00
|
|
|
PROGRAM_NAME = "Deluge"
|
2007-06-14 21:10:22 +00:00
|
|
|
PROGRAM_VERSION = "0.5.2"
|
2007-03-26 21:13:14 +00:00
|
|
|
|
|
|
|
CLIENT_CODE = "DE"
|
2007-06-14 21:10:22 +00:00
|
|
|
CLIENT_VERSION = "".join(PROGRAM_VERSION.split('.'))+"0"*(4 - len(PROGRAM_VERSION.split('.')))
|
2007-02-12 23:12:08 +00:00
|
|
|
|
2007-02-19 22:50:03 +00:00
|
|
|
CONFIG_DIR = xdg.BaseDirectory.save_config_path('deluge')
|
2007-02-14 22:43:48 +00:00
|
|
|
|
2007-02-26 23:56:17 +00:00
|
|
|
# the necessary substitutions are made at installation time
|
|
|
|
INSTALL_PREFIX = '@datadir@'
|
2007-03-22 21:52:56 +00:00
|
|
|
GLADE_DIR = os.path.join(INSTALL_PREFIX, 'share', 'deluge', 'glade')
|
|
|
|
PIXMAP_DIR = os.path.join(INSTALL_PREFIX, 'share', 'deluge', 'pixmaps')
|
|
|
|
PLUGIN_DIR = os.path.join(INSTALL_PREFIX, 'share', 'deluge', 'plugins')
|
2007-02-12 23:12:08 +00:00
|
|
|
|
2007-02-12 19:36:16 +00:00
|
|
|
def estimate_eta(state):
|
2007-07-11 04:22:44 +00:00
|
|
|
try:
|
|
|
|
return ftime(get_eta(state["total_size"], state["total_done"], state["download_rate"]))
|
|
|
|
except ZeroDivisionError:
|
|
|
|
return _("Infinity")
|
|
|
|
|
2007-07-11 02:25:19 +00:00
|
|
|
def get_eta(size, done, speed):
|
2007-07-11 04:22:44 +00:00
|
|
|
if (size - done) == 0:
|
|
|
|
raise ZeroDivisionError
|
|
|
|
return (size - done) / speed
|
2007-02-12 19:36:16 +00:00
|
|
|
|
2007-02-06 21:21:09 +00:00
|
|
|
# Returns formatted string describing filesize
|
|
|
|
# fsize_b should be in bytes
|
|
|
|
# Returned value will be in either KB, MB, or GB
|
|
|
|
def fsize(fsize_b):
|
2007-07-11 04:22:44 +00:00
|
|
|
fsize_kb = float (fsize_b / 1024.0)
|
|
|
|
if fsize_kb < 1000:
|
|
|
|
return _("%.1f KiB")%fsize_kb
|
|
|
|
fsize_mb = float (fsize_kb / 1024.0)
|
|
|
|
if fsize_mb < 1000:
|
|
|
|
return _("%.1f MiB")%fsize_mb
|
|
|
|
fsize_gb = float (fsize_mb / 1024.0)
|
|
|
|
return _("%.1f GiB")%fsize_gb
|
2007-02-06 21:21:09 +00:00
|
|
|
|
|
|
|
# Returns a formatted string representing a percentage
|
|
|
|
def fpcnt(dec):
|
2007-07-11 04:22:44 +00:00
|
|
|
return '%.2f%%'%(dec * 100)
|
2007-02-06 21:21:09 +00:00
|
|
|
|
2007-07-11 02:25:19 +00:00
|
|
|
# Returns a formatted string representing transfer speed
|
|
|
|
def fspeed(bps):
|
2007-07-11 04:22:44 +00:00
|
|
|
return '%s/s'%(fsize(bps))
|
2007-02-06 21:21:09 +00:00
|
|
|
|
|
|
|
def fseed(state):
|
2007-07-11 04:22:44 +00:00
|
|
|
return str(str(state['num_seeds']) + " (" + str(state['total_seeds']) + ")")
|
|
|
|
|
2007-02-06 21:21:09 +00:00
|
|
|
def fpeer(state):
|
2007-07-11 04:22:44 +00:00
|
|
|
return str(str(state['num_peers']) + " (" + str(state['total_peers']) + ")")
|
|
|
|
|
2007-02-12 19:36:16 +00:00
|
|
|
def ftime(seconds):
|
2007-07-11 04:22:44 +00:00
|
|
|
if seconds < 60:
|
|
|
|
return '%ds'%(seconds)
|
|
|
|
minutes = int(seconds/60)
|
|
|
|
seconds = seconds % 60
|
|
|
|
if minutes < 60:
|
|
|
|
return '%dm %ds'%(minutes, seconds)
|
|
|
|
hours = int(minutes/60)
|
|
|
|
minutes = minutes % 60
|
|
|
|
if hours < 24:
|
|
|
|
return '%dh %dm'%(hours, minutes)
|
|
|
|
days = int(hours/24)
|
|
|
|
hours = hours % 24
|
|
|
|
if days < 7:
|
|
|
|
return '%dd %dh'%(days, hours)
|
|
|
|
weeks = int(days/7)
|
|
|
|
days = days % 7
|
|
|
|
if weeks < 10:
|
|
|
|
return '%dw %dd'%(weeks, days)
|
|
|
|
return 'unknown'
|
2007-02-06 21:21:09 +00:00
|
|
|
|
|
|
|
|
2006-11-28 22:28:37 +00:00
|
|
|
def get_glade_file(fname):
|
2007-07-11 04:22:44 +00:00
|
|
|
return os.path.join(GLADE_DIR, fname)
|
2006-11-28 22:28:37 +00:00
|
|
|
|
|
|
|
def get_pixmap(fname):
|
2007-07-11 04:22:44 +00:00
|
|
|
return os.path.join(PIXMAP_DIR, fname)
|
|
|
|
|
2007-03-08 20:08:25 +00:00
|
|
|
def open_url_in_browser(dialog, link):
|
2007-07-11 04:22:44 +00:00
|
|
|
try:
|
|
|
|
webbrowser.open(link)
|
|
|
|
except webbrowser.Error:
|
|
|
|
print _("Error: no webbrowser found")
|
|
|
|
|
2007-06-06 22:26:52 +00:00
|
|
|
# Encryption States
|
|
|
|
class EncState:
|
2007-07-11 04:22:44 +00:00
|
|
|
forced, enabled, disabled = range(3)
|
|
|
|
|
2007-06-06 22:26:52 +00:00
|
|
|
class EncLevel:
|
2007-07-11 04:22:44 +00:00
|
|
|
plaintext, rc4, both = range(3)
|
2007-06-16 10:56:09 +00:00
|
|
|
|
|
|
|
class ProxyType:
|
2007-07-11 04:22:44 +00:00
|
|
|
none, socks4, socks5, socks5_pw, http, http_pw = range(6)
|