Пример #1
0
class DownloadProcessPage(Page):
    _FootMsg = ["Nav.", "", "", "Back", ""]
    _Downloader = None
    _DownloaderTimer = -1
    _Value = 0

    _URL = ""
    _DST_DIR = ""

    _PngSize = {}

    _FileNameLabel = None
    _SizeLabel = None

    _URLColor = MySkinManager.GiveColor('URL')
    _TextColor = MySkinManager.GiveColor('Text')

    def __init__(self):
        Page.__init__(self)
        self._Icons = {}
        self._CanvasHWND = None

    def Init(self):
        self._PosX = self._Index * self._Screen._Width
        self._Width = self._Screen._Width
        self._Height = self._Screen._Height

        self._CanvasHWND = self._Screen._CanvasHWND

        self._PngSize["bg"] = (48, 79)
        self._PngSize["needwifi_bg"] = (253, 132)

        bgpng = IconItem()
        bgpng._ImgSurf = MyIconPool._Icons["rom_download"]
        bgpng._MyType = ICON_TYPES["STAT"]
        bgpng._Parent = self
        bgpng.Adjust(0, 0, self._PngSize["bg"][0], self._PngSize["bg"][1], 0)
        self._Icons["bg"] = bgpng

        needwifi_bg = IconItem()
        needwifi_bg._ImgSurf = MyIconPool._Icons["needwifi_bg"]
        needwifi_bg._MyType = ICON_TYPES["STAT"]
        needwifi_bg._Parent = self
        needwifi_bg.Adjust(0, 0, self._PngSize["needwifi_bg"][0],
                           self._PngSize["needwifi_bg"][1], 0)

        self._Icons["needwifi_bg"] = needwifi_bg

        self._FileNameLabel = Label()
        self._FileNameLabel.SetCanvasHWND(self._CanvasHWND)
        self._FileNameLabel.Init("", MyLangManager.TrFont("varela12"))

        self._SizeLabel = Label()
        self._SizeLabel.SetCanvasHWND(self._CanvasHWND)
        self._SizeLabel.Init("0/0Kb", MyLangManager.TrFont("varela12"))
        self._SizeLabel.SetColor(self._URLColor)

    def OnExitCb(self, event):
        print("DownloadProcessPage OnExitCb")
        if self._Downloader == None:
            return
        try:
            self._Downloader.stop()
        except:
            pass
        return

    def GObjectUpdateProcessInterval(self):
        if self._Screen.CurPage() == self:
            if self._Downloader.isFinished():
                if self._Downloader.isSuccessful():
                    print("Success!")
                    # Do something with obj.get_dest()
                    filename = os.path.basename(self._Downloader.get_dest())
                    cur_dir = os.getcwd()

                    if filename.endswith(".zip"):
                        os.chdir(self._DST_DIR)
                        os.system("unzip " + filename)

                    elif filename.endswith(".zsync"):
                        os.chdir(self._DST_DIR)
                        os.system("rm -rf " + filename)

                    elif filename.endswith(".tar.xz"):
                        os.chdir(self._DST_DIR)
                        os.system("tar xf " + filename)
                        os.system("rm -rf " + filename)

                    os.chdir(cur_dir)
                    self.ReturnToUpLevelPage()
                    self._Screen.Draw()
                    self._Screen.SwapAndShow()

                else:
                    print("Download failed with the following exceptions:")
                    for e in self._Downloader.get_errors():
                        print(unicode(e))

                    try:
                        self._Downloader.stop()
                    except:
                        pass

                    self._Screen._MsgBox.SetText("DownloadFailed")
                    self._Screen._MsgBox.Draw()
                    self._Screen.SwapAndShow()
                    return False
            else:
                self._Value = self._Downloader.get_progress()

                filename = os.path.basename(self._Downloader.get_dest())
                self._FileNameLabel.SetText(filename)

                downloaded = self._Downloader.progress["downloaded"]
                total = self._Downloader.progress["total"]

                downloaded = downloaded / 1000.0 / 1000.0
                total = total / 1000.0 / 1000.0

                self._SizeLabel.SetText("%.2f" % downloaded + "/" +
                                        "%.2f" % total + "Mb")

                print("Progress: %d%%" % (self._Value))
                self._Screen.Draw()
                self._Screen.SwapAndShow()
                return True
        else:
            return False

    def StartDownload(self, url, dst_dir):
        if is_wifi_connected_now() == False:
            return

        if validators.url(url) and os.path.isdir(dst_dir):
            self._URL = url
            self._DST_DIR = dst_dir
        else:
            self._Screen._MsgBox.SetText("Invaid")
            self._Screen._MsgBox.Draw()
            self._Screen.SwapAndShow()
            print("url or dst dir error")
            return

        self._Downloader = Download(url, dst_dir, None)
        self._Downloader.start()

        self._DownloaderTimer = gobject.timeout_add(
            100, self.GObjectUpdateProcessInterval)

    def KeyDown(self, event):
        if IsKeyMenuOrB(event.key):
            gobject.source_remove(self._DownloaderTimer)
            self._DownloaderTimer = -1

            if self._Downloader != None:
                try:
                    self._Downloader.stop()
                except:
                    print("user canceled ")

            self.ReturnToUpLevelPage()
            self._Screen.Draw()
            self._Screen.SwapAndShow()

    def Draw(self):
        self.ClearCanvas()

        if is_wifi_connected_now() == False:
            self._Icons["needwifi_bg"].NewCoord(self._Width / 2,
                                                self._Height / 2)
            self._Icons["needwifi_bg"].Draw()
            return

        self._Icons["bg"].NewCoord(self._Width / 2, self._Height / 2 - 20)
        self._Icons["bg"].Draw()

        percent = self._Value
        if percent < 10:
            percent = 10

        rect_ = midRect(self._Width / 2, self._Height / 2 + 33, 170, 17, Width,
                        Height)
        aa_round_rect(self._CanvasHWND, rect_,
                      MySkinManager.GiveColor('TitleBg'), 5, 0,
                      MySkinManager.GiveColor('TitleBg'))

        rect2 = midRect(self._Width / 2, self._Height / 2 + 33,
                        int(170 * (percent / 100.0)), 17, Width, Height)
        rect2.left = rect_.left
        rect2.top = rect_.top
        aa_round_rect(self._CanvasHWND, rect2,
                      MySkinManager.GiveColor('Front'), 5, 0,
                      MySkinManager.GiveColor('Front'))

        rect3 = midRect(self._Width / 2, self._Height / 2 + 53,
                        self._FileNameLabel._Width,
                        self._FileNameLabel._Height, Width, Height)

        rect4 = midRect(self._Width / 2, self._Height / 2 + 70,
                        self._SizeLabel._Width, self._SizeLabel._Height, Width,
                        Height)

        self._FileNameLabel.NewCoord(rect3.left, rect3.top)
        self._SizeLabel.NewCoord(rect4.left, rect4.top)

        self._FileNameLabel.Draw()
        self._SizeLabel.Draw()
Пример #2
0
class LoadHousePage(Page):
    _FootMsg = ["Nav.", "", "", "Back", "Cancel"]
    _Value = 0
    _URL = None
    _ListFontObj = MyLangManager.TrFont("varela18")
    _URLColor = MySkinManager.GiveColor('URL')
    _TextColor = MySkinManager.GiveColor('Text')
    _Caller = None
    _img = None
    _Downloader = None
    _DownloaderTimer = -1

    def __init__(self):
        Page.__init__(self)
        self._Icons = {}
        self._CanvasHWND = None

    def Init(self):
        self._PosX = self._Index * self._Screen._Width
        self._Width = self._Screen._Width
        self._Height = self._Screen._Height

        self._CanvasHWND = self._Screen._CanvasHWND
        self._LoadingLabel = Label()
        self._LoadingLabel.SetCanvasHWND(self._CanvasHWND)
        self._LoadingLabel.Init("Loading", self._ListFontObj)
        self._LoadingLabel.SetColor(self._TextColor)

    def OnLoadCb(self):
        if self._URL is None:
            return
        self._img = None
        self.ClearCanvas()
        self._Screen.Draw()
        self._Screen.SwapAndShow()

        filename = self._URL.split("/")[-1].strip()
        local_dir = self._URL.split("raw.githubusercontent.com")

        if len(local_dir) > 1:
            menu_file = local_dir[1]
            local_menu_file = "%s/aria2download%s" % (os.path.expanduser('~'),
                                                      menu_file)

            if FileExists(local_menu_file):
                #load json
                with open(local_menu_file) as json_file:
                    try:
                        local_menu_json = json.load(json_file)
                        self._Caller._MyStack.Push(local_menu_json["list"])
                    except:
                        pass

                    self.Leave()

            else:
                self._Downloader = Download(self._URL, "/tmp", None)
                self._Downloader.start()
                self._DownloaderTimer = gobject.timeout_add(
                    400, self.GObjectUpdateProcessInterval)

    def GObjectUpdateProcessInterval(self):
        ret = True
        if self._Screen.CurPage() == self:
            if self._Downloader._stop == True:
                ret = False

            dst_filename = self._Downloader.get_dest()
            if self._Downloader.isFinished():
                if self._Downloader.isSuccessful():
                    filename = self._URL.split("/")[-1].strip()
                    local_dir = self._URL.split("raw.githubusercontent.com")
                    menu_file = local_dir[1]
                    local_menu_file = "%s/aria2download%s" % (
                        os.path.expanduser('~'), menu_file)

                    dl_file = os.path.join("/tmp", filename)
                    if not os.path.exists(os.path.dirname(local_menu_file)):
                        os.makedirs(os.path.dirname(local_menu_file))

                    copyfile(dl_file, local_menu_file)
                    with open(local_menu_file) as json_file:
                        try:
                            local_menu_json = json.load(json_file)
                            self._Caller._MyStack.Push(local_menu_json["list"])
                        except:
                            pass

                    ret = False

                    self.Leave()
                else:
                    self._Screen._MsgBox.SetText("Fetch house failed")
                    self._Screen._MsgBox.Draw()
                    self._Screen.SwapAndShow()
                    ret = False
            return ret
        else:
            return False

    def Leave(self):
        if self._DownloaderTimer != -1:
            gobject.source_remove(self._DownloaderTimer)
        self._DownloaderTimer = -1

        if self._Downloader != None:
            try:
                self._Downloader.stop()
            except:
                print("user canceled ")

        self.ReturnToUpLevelPage()
        self._Screen.Draw()
        self._Screen.SwapAndShow()
        self._URL = None

    def KeyDown(self, event):
        if IsKeyMenuOrB(event.key):
            self.Leave()

    def Draw(self):
        self.ClearCanvas()
        self._LoadingLabel.NewCoord((Width - self._LoadingLabel._Width) / 2,
                                    (Height - 44) / 2)
        self._LoadingLabel.Draw()

        if self._img is not None:
            self._CanvasHWND.blit(
                self._img,
                midRect(Width / 2, (Height - 44) / 2,
                        pygame.Surface.get_width(self._img),
                        pygame.Surface.get_height(self._img), Width,
                        Height - 44))