<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      eventCookie="@@@eventCookie@@@" dtvPlatform="@@@dtvPlatform@@@">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<link href="resource:css/main.css" rel="stylesheet" type="text/css" />
<link href="resource:css/download.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="resource:templates/osxdnd.js" />
<t:include filename="dynamic.js" />
<t:include filename="settings.js" />

    <t:execOnLoad><![CDATA[
from miro import config
from miro import prefs
from miro import util
from miro import downloader
from miro.gtcache import gettext as _
from miro.platform.utils import getAvailableBytesForMovies

def getFreeSpace():
    bytes = getAvailableBytesForMovies()
    return util.formatSizeForUser(bytes, "0B", False)

def getAvailableSpace():
    bytes = getAvailableBytesForMovies()
    if config.get(prefs.PRESERVE_DISK_SPACE):
        preserved = config.get(prefs.PRESERVE_X_GB_FREE) * 1024 * 1024 * 1024
        bytes -= preserved
    return util.formatSizeForUser(bytes, "0B", False)

def getUpRate():
    return _("%0.1f KB/s uploading") % (downloader.totalUpRate / 1024.0,)

def getDownRate():
    return _("%0.1f KB/s downloading") % (downloader.totalDownRate / 1024.0,)

global searchFilter
searchFilter = lambda x: filters.matchingItems(x, None)

def makeDownloadCategoryView(cat):
    return views.downloadingItems.filterWithIndex(indexes.downloadsByCategory, cat)
allNormalDownloads = makeDownloadCategoryView('normal')
allExternalDownloads = views.items.filter(lambda x: x.getFeed().url == 'dtv:manualFeed' and x.isNonVideoFile())
# Specifically don't include anything in external downloads in uploads.
allUploads = views.items.filter(lambda x: x.downloader and x.downloader.getState() == 'uploading' and not (x.getFeed().url == 'dtv:manualFeed' and x.isNonVideoFile()))
allPendingDownloads = makeDownloadCategoryView('pending')
allPausedDownloads = views.pausedItems.filter (lambda x: not (x.getFeed().url == 'dtv:manualFeed' and x.isNonVideoFile()))

matchingNormalDLs = allNormalDownloads.filter(lambda x:searchFilter(x),sortFunc=sorts.itemSortDownloads.sort,resort=True)
matchingExternalDLs = allExternalDownloads.filter(lambda x:searchFilter(x))
matchingULs = allUploads.filter(lambda x:searchFilter(x),sortFunc=sorts.itemSortSeedingTorrents.sort,resort=True)
matchingPendingDLs = allPendingDownloads.filter(lambda x:searchFilter(x),sortFunc=sorts.itemSortDownloads.sort,resort=True)
matchingPausedDLs = allPausedDownloads.filter(lambda x:searchFilter(x),sortFunc=sorts.itemSortDownloads.sort,resort=True)

def updateSearchString(newSearch):
    global searchFilter
    if len(newSearch) == 0:
        searchFilter = lambda x: filters.matchingItems(x, None)
    else:
        searchFilter = lambda x: filters.matchingItems(x, newSearch)
    allNormalDownloads.recomputeFilter(matchingNormalDLs)
    allExternalDownloads.recomputeFilter(matchingExternalDLs)
    allUploads.recomputeFilter(matchingULs)
    allPendingDownloads.recomputeFilter(matchingPendingDLs)
    allPausedDownloads.recomputeFilter(matchingPausedDLs)

def setSortBy(by, which, handle):
    if which == 'main':
        sorts.itemSortDownloads.setSortBy(by)
        allNormalDownloads.recomputeSort(matchingNormalDLs)
        allPendingDownloads.recomputeSort(matchingPendingDLs)
        allPausedDownloads.recomputeSort(matchingPausedDLs)
    else:
        sorts.itemSortSeedingTorrents.setSortBy(by)
        allUploads.recomputeSort(matchingULs)
    handle.forceUpdate()
]]>
    </t:execOnLoad>
    <t:execOnUnload><![CDATA[
allNormalDownloads.unlink()
allExternalDownloads.unlink()
allUploads.unlink()
allPendingDownloads.unlink()
allPausedDownloads.unlink()
]]>
    </t:execOnUnload>
</head>

<body onkeydown="sendKeyToSearchBox(event);">

<!-- TITLE BAR ************************************************************ -->
<div id="main-titlebar">
	<div id="main-icon" class="noborder"><img src="resource:images/download-icon.png" /></div>
        <h1 id="main-title" i18n:translate="">Downloading</h1>
        <div id="search-box-container">
            <input id="search-box" type="search" onfocus="onSearchFocus(this)" onblur="endEditSearch()" value="***_('Find')***"/>
        </div>
        <div id="downloads-global-actions">
            <div><p t:hideIf="views.downloadingItems.len() == 0" t:updateHideOnView="views.downloadingItems"><a href="#" onclick="return eventURL('action:pauseAll');" title="Pause All"><img src="resource:images/pause-all-downloads.png" /><br /><span i18n:translate="">Pause All</span></a></p>&nbsp;</div>
            <div><p t:hideIf="views.pausedItems.len() == 0" t:updateHideOnView="views.pausedItems"><a href="#" onclick="return eventURL('action:resumeAll');" title="Resume All"><img src="resource:images/resume-all-downloads.png" /><br /><span i18n:translate="">Resume All</span></a></p>&nbsp;</div>
        </div>

        <div id="download-info">
            <ul>
                <li>
                    <span i18n:translate=""><span i18n:name="size" t:replace="getFreeSpace()" /> free on disk</span>
                </li>
                <li>
                    <span t:updateForConfigChange="">
                        <span t:hideIf="getFreeSpace() == getAvailableSpace()">
                            <span i18n:translate=""><span i18n:name="size" t:replace="getAvailableSpace()" /> free for downloads</span>
                        </span>
                    </span>
                </li>
            </ul>
            <div t:updateForView="views.remoteDownloads">
                <ul class="download-rates">
                    <li t:showIf="downloader.totalDownRate > 1">
                        <span t:replace="getDownRate()"/>
                    </li>
                    <li t:showIf="downloader.totalUpRate > 1">
                        <span t:replace="getUpRate()"/>
                    </li>
                </ul>
            </div>
        </div>
</div>
<!-- / TITLE BAR -->

<div id="main-container">
        <div t:hideIf="allNormalDownloads.len() == 0" t:updateHideOnView="allNormalDownloads" class="main-channelbar downloading-channelbar no-arrow">
                <span i18n:translate="">Channel Downloads</span>
        </div>

    <!-- SORT BAR ************************************************************* --> 
    <div t:updateForView="views.downloadingItems"> 
        <div t:hideIf="matchingNormalDLs.len() + matchingPendingDLs.len() + matchingPausedDLs.len() == 0">
            <t:executeTemplate filename="static-tab-sort-bar" 
                    itemSort="sorts.itemSortDownloads"
                    section="'main'" />
        </div>
    </div> 
    <!-- / SORT BAR -->

	<!-- VIDEOS ************************************************** -->
    <div t:repeatForView="matchingNormalDLs" t:repeatTemplate='download-item' t:containerDiv="true" />
    <div t:repeatForView="matchingPendingDLs" t:repeatTemplate='download-item' t:containerDiv="true" />
    <div t:repeatForView="matchingPausedDLs" t:repeatTemplate='download-item' t:containerDiv="true" />
	<!-- / VIDEOS -->
    <div t:updateForView="matchingExternalDLs">
        <div t:hideIf="matchingExternalDLs.len() == 0">
            <div class="main-channelbar downloading-channelbar no-arrow">
                <span i18n:translate="">External Downloads</span>
                <a i18n:translate="" href="#" onclick="return eventURL('action:clearTorrents');" class="clear-completed-link" title="Clear Completed and Failed Downloads">Clear Completed and Failed Downloads</a>
            </div>
        </div>
    </div> 
    <div t:repeatForView="matchingExternalDLs" t:repeatTemplate='download-item' t:containerDiv="true" />

    <div t:updateForView="matchingULs">
        <div t:hideIf="matchingULs.len() == 0">
            <div class="main-channelbar seeding-channelbar no-arrow">
                <span i18n:translate="">Seeding Torrents</span>
            </div>
            <t:executeTemplate filename="static-tab-sort-bar" 
                    itemSort="sorts.itemSortSeedingTorrents"
                    section="'seeding-torrents'" />
        </div>
    </div>
    <div t:repeatForView="matchingULs" t:repeatTemplate='download-item' t:containerDiv="true" />
</div>

</body>
</html>
