########################################################################
#
#  Copyright (C) 2009, CZ.NIC, z.s.p.o.
# 
#  This file is part of dnstester.
#
#  dnstester 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 3 of the License, or
#  (at your option) any later version.
#
#  dnstester 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, see <http://www.gnu.org/licenses/>.
#
########################################################################

import pygtk
pygtk.require("2.0")
import gtk, gobject
import threading, thread

import lang, config, upload
from page import Page
import images
from errorbox import ErrorBox

class PageUploadProgress(Page):

    title = 'upload_title'
    number = 7
    
    def __init__(self, parent):
        Page.__init__(self, parent)
        self.widget = self.create_vbox()

    def create_vbox(self):
        vbox = gtk.VBox()
        vbox.set_border_width(20)
        vbox.set_spacing(20)
        
        # add frame with label
        self.label = gtk.Label()
        lang.bind(self.label, 'testupload')
        vbox.pack_start(self.label, False, False)

        self.vrtule = gtk.Image()
        self.vrtule.set_from_file(images.fullname('loader.gif'))
        self.vrtule.show()
        vbox.pack_start(self.vrtule, False, False)

        vbox.show_all()
        return vbox


    def on_before_show(self):
        lang.bind(self.label, 'testupload')
        self.vrtule.show()
        self.start_upload()


    def start_upload(self):
        self.upload_thread = threading.Thread(target=self.do_upload_async)
        self.upload_thread.start()
        self.parent.enable_back_button(False)


    def do_upload_async(self):
        r = upload.gathering(self.parent)
        gobject.idle_add(self.upload_finished, r)


    def upload_finished(self, result):
        self.result = result
        self.vrtule.hide()
        self.parent.set_page_complete(self.widget, True)
        self.parent.next()

        if not result:
            ErrorBox(self.parent, 'upload_failed')

