# -*- coding: UTF-8 -*-

#Boa:Frame:wxFrame1

from wxPython.wx import *
from wxPython.lib.buttons import *
from wxPython.gizmos import *
#from ID3 import *
import ogg.vorbis
import thread
import os.path
from os.path import basename
import cPickle
import time
from xml.dom.minidom import Document
from Lists import Lists
import traceback

from upload import upload
from rdfDc import rdfDc

wxEVT_UPDATE_PROGRESS = wxNewEventType()

def logTraceback():
    if(os.name == "nt"):
        elog=open("rut-error.log","w")
        traceback.print_exc(None,elog)
        elog.close()
    else:
        traceback.print_exc()


def EVT_UPDATE_PROGRESS(win, func):
    win.Connect(-1, -1, wxEVT_UPDATE_PROGRESS, func)


def create(parent):
    return wxFrame1(parent)

[
 wxID_WXFRAME1,
 wxID_WXFRAME1DC__TITLE__ALTERNATIVE,
 wxID_WXFRAME1BUTTON2,
 wxID_WXFRAME1DC__TITLE,
 wxID_WXFRAME1DC__TYPE,
 wxID_WXFRAME1DATE_YY,
 wxID_WXFRAME1DATE_DD,
 wxID_WXFRAME1DATE_MM,
 wxID_WXFRAME1DC__LANGUAGE,
 wxID_WXFRAME1DC__COVERAGE__SPATIAL,
 wxID_WXFRAME1DC__COVERAGE__TEMPORAL,
 wxID_WXFRAME1DC__RIGHTS,
 wxID_WXFRAME1DC__RIGHTS__LICENSE,
 wxID_WXFRAME1DC__CREATOR,
 wxID_WXFRAME1DC__CREATOR__ROLE,
 wxID_WXFRAME1DC__CONTRIBUTOR,
 wxID_WXFRAME1DC__CONTRIBUTOR__ROLE,
 wxID_WXFRAME1DC__DESCRIPTION__ABSTRACT,
 wxID_WXFRAME1DC__SUBJECT,
 wxID_WXFRAME1DC__DESCRIPTION__TABLEOFCONTENT,
 wxID_WXFRAME1DC__DESCRIPTION__LONG,
 wxID_WXFRAME1GAUGE1,
 wxID_WXFRAME1BUTTON1,
 wxID_WXFRAME1BUTTON3,
 wxID_WXFRAME1INQUEUE,
 wxID_WXFRAME1UploadSpeed,
 wxID_WXFRAME1NOTEBOOK1,
 wxID_WXFRAME1PANEL1,
 wxID_WXFRAME1PANEL2,
 wxID_WXFRAME1PASSWORD,
 wxID_WXFRAME1SERVERNAME,
 wxID_WXFRAME1STATICBOX1,
 wxID_WXFRAME1STATICBOX2,
 wxID_WXFRAME1STATICBOX3,
 wxID_WXFRAME1STATICTEXT1,
 wxID_WXFRAME1STATICTEXT10,
 wxID_WXFRAME1STATICTEXT11,
 wxID_WXFRAME1STATICTEXT12,
 wxID_WXFRAME1STATICTEXT13,
 wxID_WXFRAME1STATICTEXT14,
 wxID_WXFRAME1STATICTEXT15,
 wxID_WXFRAME1STATICTEXT16,
 wxID_WXFRAME1STATICTEXT17,
 wxID_WXFRAME1STATICTEXT18,
 wxID_WXFRAME1STATICTEXT19,
 wxID_WXFRAME1STATICTEXT2,
 wxID_WXFRAME1STATICTEXT20,
 wxID_WXFRAME1STATICTEXT21,
 wxID_WXFRAME1STATICTEXT22,
 wxID_WXFRAME1infotext,
 wxID_WXFRAME1STATICTEXT3,
 wxID_WXFRAME1STATICTEXT4,
 wxID_WXFRAME1STATICTEXT5,
 wxID_WXFRAME1STATICTEXT6,
 wxID_WXFRAME1STATICTEXT7,
 wxID_WXFRAME1STATICTEXT8,
 wxID_WXFRAME1STATICTEXT9,
 wxID_WXFRAME1USERNAME,
 wxID_WXSTATUSBAR,
] = map(lambda _init_ctrls: wxNewId(), range(59))

class wxFrame1(wxFrame):
    def errDialog(self,msg):
        dlg = wxMessageDialog(self, msg, 'Reboot Upload Tool: Error', wxOK | wxICON_INFORMATION)
        try:
            dlg.ShowModal()
        finally:
            dlg.Destroy()

    def errInfo(self,msg):
        dlg = wxMessageDialog(self, msg, 'Reboot Upload Tool: Info', wxOK | wxICON_INFORMATION)
        try:
            dlg.ShowModal()
        finally:
            dlg.Destroy()

    def _init_coll_notebook1_Pages(self, parent):
        # generated method, don't edit

        if(self.defaultpanel == 1):
            parent.AddPage(imageId=-1, page=self.panel1, select=True, text='Preferences & Status')
            parent.AddPage(imageId=-1, page=self.panel2, select=False, text='Meta editing & Enqueue')
            
        else:
            parent.AddPage(imageId=-1, page=self.panel1, select=False, text='Preferences & Status')
            parent.AddPage(imageId=-1, page=self.panel2, select=True, text='Meta editing & Enqueue')
        
        

    def _init_utils(self):
        # generated method, don't edit
        pass

    def _init_ctrls(self, prnt):
        lists = Lists()
        #some are edited
        wxFrame.__init__(self, id=wxID_WXFRAME1, name='', parent=prnt,size=wxSize(790, 605),
              style=wxDEFAULT_FRAME_STYLE, title='rut - Reboot Upload Tool')
        self._init_utils()
        self.SetClientSize(wxSize(790, 605))

        self.notebook1 = wxNotebook(id=wxID_WXFRAME1NOTEBOOK1, name='notebook1',
              parent=self, pos=wxPoint(0, 0), size=wxSize(790, 605), style=0)
      
        self.statusBar= self.CreateStatusBar(id=wxID_WXSTATUSBAR, name='statusBar',style=0)    
        
        self.statusBar.SetFieldsCount(1)
        self.statusBar.SetStatusText("",0)

        self.panel1 = wxPanel(id=wxID_WXFRAME1PANEL1, name='panel1',
              parent=self.notebook1, pos=wxPoint(0, 0), size=wxSize(786, 556),
              style=wxTAB_TRAVERSAL)

        self.panel2 = wxPanel(id=wxID_WXFRAME1PANEL2, name='panel2',
              parent=self.notebook1, pos=wxPoint(0, 0), size=wxSize(786, 556),
              style=wxTAB_TRAVERSAL)

        self.staticBox2 = wxStaticBox(id=wxID_WXFRAME1STATICBOX2,
              label='Upload Queue Status', name='staticBox2', parent=self.panel1,
              pos=wxPoint(24, 370), size=wxSize(736, 170), style=0)

        self.staticBox3 = wxStaticBox(id=wxID_WXFRAME1STATICBOX3,
            label='Messages', name='staticBox3', parent=self.panel1,
            pos=wxPoint(24, 90), size=wxSize(736, 170), style=0)

        self.infotext = wxStaticText(id=wxID_WXFRAME1infotext,
              label='', name='infotext', parent=self.panel1,
              pos=wxPoint(32, 110),style=0)


        self.staticText1 = wxStaticText(id=wxID_WXFRAME1STATICTEXT1,
              label='Title', name='staticText1', parent=self.panel2,
              pos=wxPoint(16, 24), style=0)

        self.staticText2 = wxStaticText(id=wxID_WXFRAME1STATICTEXT2,
              label='Selected files', name='staticText2',
              parent=self.panel2, pos=wxPoint(16, 48), 
              style=0)

        self.staticText3 = wxStaticText(id=wxID_WXFRAME1STATICTEXT3,
              label='Title of the series', name='staticText3',
              parent=self.panel2, pos=wxPoint(16, 72), 
              style=0)

        self.staticText4 = wxStaticText(id=wxID_WXFRAME1STATICTEXT4,
              label='Type of Emission', name='staticText4',
              parent=self.panel2, pos=wxPoint(16, 96),
              style=0)

        self.staticText5 = wxStaticText(id=wxID_WXFRAME1STATICTEXT5,
              label='Creation date (yyyy-mm-dd)', name='staticText5',
              parent=self.panel2, pos=wxPoint(16, 120),
              style=0)

        self.staticText6 = wxStaticText(id=wxID_WXFRAME1STATICTEXT6,
              label='License holder', name='staticText6', parent=self.panel2,
              pos=wxPoint(408, 72), style=0)

        self.staticText7 = wxStaticText(id=wxID_WXFRAME1STATICTEXT7,
              label='Name of an executive person', name='staticText7',
              parent=self.panel2, pos=wxPoint(408, 120),
              style=0)

        self.staticText8 = wxStaticText(id=wxID_WXFRAME1STATICTEXT8,
              label='Name of an assistent', name='staticText8',
              parent=self.panel2, pos=wxPoint(408, 168),
              style=0)

        self.staticText9 = wxStaticText(id=wxID_WXFRAME1STATICTEXT9,
              label='Brief description or subtitle', name='staticText9',
              parent=self.panel2, pos=wxPoint(16, 216), 
              style=0)

        self.staticText10 = wxStaticText(id=wxID_WXFRAME1STATICTEXT10,
              label='Table of content or playlist (title,artist,album,label,length)',
              name='staticText10', parent=self.panel2, pos=wxPoint(16, 288),
              style=0)

        self.staticText11 = wxStaticText(id=wxID_WXFRAME1STATICTEXT11,
              label='Role of assistent', name='staticText11',
              parent=self.panel2, pos=wxPoint(408, 192),
              style=0)

        self.staticText12 = wxStaticText(id=wxID_WXFRAME1STATICTEXT12,
              label='Role of executive', name='staticText12',
              parent=self.panel2, pos=wxPoint(408, 144),
              style=0)

        self.staticText13 = wxStaticText(id=wxID_WXFRAME1STATICTEXT13,
              label='Related time period', name='staticText13',
              parent=self.panel2, pos=wxPoint(16, 192),
              style=0)

        self.staticText14 = wxStaticText(id=wxID_WXFRAME1STATICTEXT14,
              label='Regional context', name='staticText14', parent=self.panel2,
              pos=wxPoint(16, 168),style=0)

        self.staticText15 = wxStaticText(id=wxID_WXFRAME1STATICTEXT15,
              label='Mostly used Language', name='staticText15',
              parent=self.panel2, pos=wxPoint(16, 144),style=0)

        self.staticText16 = wxStaticText(id=wxID_WXFRAME1STATICTEXT16,
              label='License', name='staticText16', parent=self.panel2,
              pos=wxPoint(408, 96), size=wxSize(52, 16), style=0)

        self.staticText17 = wxStaticText(id=wxID_WXFRAME1STATICTEXT17,
              label='Keywords, may be build either from the title or description entries',
              name='staticText17', parent=self.panel2, pos=wxPoint(408, 216),
              style=0)

        self.staticText18 = wxStaticText(id=wxID_WXFRAME1STATICTEXT18,
              label='Long description', name='staticText18', parent=self.panel2,
              pos=wxPoint(408, 288), style=0)

#X
        self.staticText19 = wxStaticText(id=wxID_WXFRAME1STATICTEXT19, 
              label='', name='staticText19', parent=self.panel1, 
              pos=wxPoint(32, 400), style=0)

        self.dc__title__alternative = wxTextCtrl(id=wxID_WXFRAME1DC__TITLE__ALTERNATIVE,
              name='dc__title__alternative', parent=self.panel2,
              pos=wxPoint(192, 16), size=wxSize(192, 24), style=0, value='')

        self.SelectFileButton = wxButton(id=wxID_WXFRAME1BUTTON2, label='File browser',
              name='SelectFileButton', parent=self.panel2, pos=wxPoint(192, 40),
              size=wxSize(192, 22), style=0)
        EVT_BUTTON(self.SelectFileButton, wxID_WXFRAME1BUTTON2, self.OnSelectFileButton)

        self.dc__title = wxTextCtrl(id=wxID_WXFRAME1DC__TITLE, name='dc__title',
              parent=self.panel2, pos=wxPoint(192, 64), size=wxSize(192, 24),
              style=0, value='')
        try:
            self.dc__type = wxComboBox(choices=lists.getContainers(self.User), id=wxID_WXFRAME1DC__TYPE,
                  name='dc__type', parent=self.panel2, pos=wxPoint(192, 88),
                  size=wxSize(192, 22), style=wxCB_READONLY, validator=wxDefaultValidator,
                  value='')
        # could be split up to know if its the internet connection or the invalid email.
        except:
            self.errDialog ('Could not connect to server. Please check your Internet Connection;\nMake sure your provided email/password are ok.')
            self.defaultpanel =1
        
        self.date_yy = wxSpinCtrl(id=wxID_WXFRAME1DATE_YY, initial=2003,
              max=2033, min=1970, name='date_yy', parent=self.panel2,
              pos=wxPoint(192, 112), size=wxSize(96, 22), style=wxSP_ARROW_KEYS,
              value='2003')
        self.date_yy.SetRange(1970, 2033)

        self.date_mm = wxSpinCtrl(id=wxID_WXFRAME1DATE_MM, initial=1, max=12,
              min=1, name='date_mm', parent=self.panel2, pos=wxPoint(288, 112),
              size=wxSize(48, 22), style=wxSP_ARROW_KEYS, value='1')
        self.date_mm.SetRange(1, 12)

        self.date_dd = wxSpinCtrl(id=wxID_WXFRAME1DATE_DD, initial=1, max=31,
              min=1, name='date_dd', parent=self.panel2, pos=wxPoint(336, 112),
              size=wxSize(48, 22), style=wxSP_ARROW_KEYS, value='1')
        self.date_dd.SetRange(1, 31)

        self.dc__language = wxComboBox(choices=lists.getLangs(), id=wxID_WXFRAME1DC__LANGUAGE,
              name='dc__language', parent=self.panel2, pos=wxPoint(192, 136),
              size=wxSize(192, 22), style=0, validator=wxDefaultValidator,
              value='')

        self.dc__coverage__spatial = wxComboBox(choices=lists.getCountries(),
              id=wxID_WXFRAME1DC__COVERAGE__SPATIAL,
              name='dc__coverage__spatial', parent=self.panel2, pos=wxPoint(192,
              160), size=wxSize(192, 22), style=0, validator=wxDefaultValidator,
              value='')

        self.dc__coverage__temporal = wxTextCtrl(id=wxID_WXFRAME1DC__COVERAGE__TEMPORAL,
              name='dc__coverage__temporal', parent=self.panel2,
              pos=wxPoint(192, 184), size=wxSize(192, 24), style=0, value='')

        self.dc__rights = wxTextCtrl(id=wxID_WXFRAME1DC__RIGHTS,
              name='dc__rights', parent=self.panel2, pos=wxPoint(576, 64),
              size=wxSize(192, 24), style=0, value='')
        self.dc__rights.SetBackgroundColour(wxColour(255,255,192))

        self.dc__rights__license = wxComboBox(choices=lists.getLicenses(),
              id=wxID_WXFRAME1DC__RIGHTS__LICENSE, name='dc__rights__license',
              parent=self.panel2, pos=wxPoint(576, 88), size=wxSize(192, 22),
              style=0, validator=wxDefaultValidator, value='')
        self.dc__rights__license.SetBackgroundColour(wxColour(255,255,192))

        self.dc__creator = wxTextCtrl(id=wxID_WXFRAME1DC__CREATOR,
              name='dc__creator', parent=self.panel2, pos=wxPoint(576, 112),
              size=wxSize(192, 24), style=0, value='')
        self.dc__creator.SetBackgroundColour(wxColour(255,192,255))

        self.dc__creator__role = wxComboBox(choices=lists.getCreatorRoles(),
              id=wxID_WXFRAME1DC__CREATOR__ROLE, name='dc__creator__role',
              parent=self.panel2, pos=wxPoint(576, 136), size=wxSize(192, 22),
              style=0, validator=wxDefaultValidator, value='')
        self.dc__creator__role.SetBackgroundColour(wxColour(255,192,255))

        self.dc__contributor = wxTextCtrl(id=wxID_WXFRAME1DC__CONTRIBUTOR,
              name='dc__contributor', parent=self.panel2, pos=wxPoint(576, 160),
              size=wxSize(192, 24), style=0, value='')
        self.dc__contributor.SetBackgroundColour(wxColour(192,255,255))

        self.dc__contributor__role = wxComboBox(choices=lists.getContributorRoles(),
              id=wxID_WXFRAME1DC__CONTRIBUTOR__ROLE,
              name='dc__contributor__role', parent=self.panel2, pos=wxPoint(576,
              184), size=wxSize(192, 22), style=0, validator=wxDefaultValidator,
              value='')
        self.dc__contributor__role.SetBackgroundColour(wxColour(192,255,255))

        self.dc__description__abstract = wxTextCtrl(id=wxID_WXFRAME1DC__DESCRIPTION__ABSTRACT,
              name='dc__description__abstract', parent=self.panel2,
              pos=wxPoint(16, 232), size=wxSize(368, 48), style=wxTE_MULTILINE,
              value='')

        self.dc__subject = wxTextCtrl(id=wxID_WXFRAME1DC__SUBJECT,
              name='dc__subject', parent=self.panel2, pos=wxPoint(408, 232),
              size=wxSize(360, 48), style=wxTE_MULTILINE, value='')

        self.dc__description__tableOfContent = wxTextCtrl(id=wxID_WXFRAME1DC__DESCRIPTION__TABLEOFCONTENT,
              name='dc__description__tableOfContent', parent=self.panel2,
              pos=wxPoint(16, 304), size=wxSize(368, 232), style=wxTE_MULTILINE,
              value='')

        self.dc__description__long = wxTextCtrl(id=wxID_WXFRAME1DC__DESCRIPTION__LONG,
              name='dc__description__long', parent=self.panel2, pos=wxPoint(408,
              304), size=wxSize(360, 232), style=wxTE_MULTILINE, value='')

        self.uploadqueue_add_button = wxButton(id=wxID_WXFRAME1BUTTON3, label='Add to Upload Queue',
              name='uploadqueue_add_button', parent=self.panel2, pos=wxPoint(576, 16),
              size=wxSize(192, 38), style=0)
        EVT_BUTTON(self.uploadqueue_add_button, wxID_WXFRAME1BUTTON3, self.OnAddToUploadQueueButton)

#        self.uploadqueue_add_button = wxButton(ID=wxID_WXFRAME1BUTTON3, label='Enque', name='uploadqueue_add_button',
#              parent=self.panel2, pos=wxPoint(576, 16), size=wxSize(192, 38), style=0)
#        EVT_BUTTON(self.uploadqueue_add_button, wxID_WXFRAME1BUTTON3, self.OnAddToUploadQueueButton)

        # Progressbar in Upload Queue
        self.progressbar = wxGauge(id=wxID_WXFRAME1GAUGE1, name='progressbar',
              parent=self.panel1, pos=wxPoint(380, 400), range=100,
              size=wxSize(296, 24), style=wxGA_HORIZONTAL|wxGA_SMOOTH,
              validator=wxDefaultValidator)

#X
#        self.UploadSpeed = wxLEDNumberCtrl(id=wxID_WXFRAME1UploadSpeed,
#              parent=self.panel1, pos=wxPoint(685, 400), size=wxSize(64, 24),
#              style=wxLED_ALIGN_LEFT)
#        self.UploadSpeed.SetValue('0')
#        self.UploadSpeed.SetLabel('')
#X
        self.UploadSpeed = wxStaticText(id=wxID_WXFRAME1UploadSpeed, label='',name='UploadSpeed',
            parent=self.panel1, pos=wxPoint(686, 405), size=wxSize(64, 24), style=0)


#X
        self.inQueueText = wxStaticText(id=wxID_WXFRAME1INQUEUE, label='-empty-',
              name='inQueue', parent=self.panel1, pos=wxPoint(32, 430),
              size=wxSize(320, 16), style=0)

        self.staticBox1 = wxStaticBox(id=wxID_WXFRAME1STATICBOX1,
              label='Archive settings', name='staticBox1', parent=self.panel1,
              pos=wxPoint(24, 8), size=wxSize(736, 56), style=0)

        self.staticText20 = wxStaticText(id=wxID_WXFRAME1STATICTEXT20,
              label='Servername', name='staticText20', parent=self.panel1,
              pos=wxPoint(32, 32), style=0)

        self.servername = wxTextCtrl(id=wxID_WXFRAME1SERVERNAME,
              name='servername', parent=self.panel1, pos=wxPoint(112, 28),
              size=wxSize(136, 22), style=0, value='archive.reboot.fm')

        self.UsernameLabel = wxStaticText(id=wxID_WXFRAME1STATICTEXT21,
              label='Email', name='UsernameLabel', parent=self.panel1,
              pos=wxPoint(256, 32), style=0)

        self.username = wxTextCtrl(id=wxID_WXFRAME1USERNAME, name='username',
              parent=self.panel1, pos=wxPoint(320, 28), size=wxSize(120, 22),
              style=0, value='')

        self.staticText22 = wxStaticText(id=wxID_WXFRAME1STATICTEXT22,
              label='Password', name='staticText22', parent=self.panel1,
              pos=wxPoint(456, 32), size=wxSize(64, 24), style=0)

        self.password = wxTextCtrl(id=wxID_WXFRAME1PASSWORD, name='password',
              parent=self.panel1, pos=wxPoint(520, 28), size=wxSize(120, 22),
              style=wxTE_PASSWORD, value='')

        self.SaveSettingsButton = wxButton(id=wxID_WXFRAME1BUTTON1,
              label='Save setting', name='SaveSettingsButton', parent=self.panel1,
              pos=wxPoint(672, 25), size=wxSize(81, 27), style=0)
        EVT_BUTTON(self.SaveSettingsButton, wxID_WXFRAME1BUTTON1, self.OnSaveSettingsButton)

        if self.defaultpanel==1:
           self.infotext.SetLabel("Please log in with your email and password. and click Save setting")

	self._init_coll_notebook1_Pages(self.notebook1)

    def __init__(self, parent):

        self.dc__identifier=''
        self.filenames = []

        self.nothread=1
        try:
            FILE = open('setting.pickle','r')
            t = cPickle.load(FILE)
            FILE.close()
            self.User = t[1]
            self.defaultpanel = 2

            self._init_ctrls(parent)

            self.servername.SetValue(t[0])
            self.username.SetValue(t[1])
            self.password.SetValue(t[2])
            self.wxEVT_UPDATE_PROGRESS = wxEVT_UPDATE_PROGRESS
            self.ul = upload(self)
            thread.start_new_thread(self.ul.startque,())
            self.nothread=0

        except:
            self.defaultpanel = 1
            self.User = ''
            self._init_ctrls(parent)
            logTraceback()
            
    # event table
        EVT_UPDATE_PROGRESS(self, self.OnUpdateProgress)


    def OnCombobox1Combobox(self, event):
        event.Skip()

    def OnCombobox2Combobox(self, event):
        event.Skip()

    def OnCombobox3Combobox(self, event):
        event.Skip()

    def OnDc__rights__licenseCombobox(self, event):
        event.Skip()

    #checks files that where selected, and gatheres some data
    def ThisIsIt(self, name):
        if os.path.isfile(name):
#            print "OK, THIS IS IT, %s" % name
            try:
                self.dc__identifier = name
                vf = ogg.vorbis.VorbisFile(name)
                try:
                    vc = vf.comment()
                    self.dc__title.SetValue(vc['TITLE'][0])
                except:
                    print "no vorbis comments found"

                try:
                    vi = vf.info()
                    self.dc__format__encoding = "ogg/vorbis %d bps, %s channels, frequenz %s" % (vi.bitrate_nominal, vi.channels, vi.rate)
                    self.dc__format__extent = "%d" % vf.time_total(-1)
                except:
                    print "can't determine vorbis format informations"
                    raise Error, "can't determine vorbis format informations"

            except:
                print "couldn't deal with the file at all. bla"
                #raise Error, "couldn't deal with the file at all."
                
                logTraceback()
                
#            try:
#                id3info = ID3(name)
#                for k, v in id3info.items():
#                     print k, ":", v
#                self.dc__title.SetValue(id3info['TITLE'])
#            except InvalidTagError, message:
#                print "Invalid ID3 tag:", message
#        self.dc__title.SetValue(self.genericDirCtrl1.GetFilePath())

    def NoReaction(self, event):
        event.Skip()

    def OnAddToUploadQueueButton(self, event):
        if (not (len(self.filenames)>0)) or (not self.dc__title__alternative.GetValue() or (not self.dc__type.GetValue())):
            self.errDialog('The fields "Title", "Select file to upload" and "Type of transmission" are required.')
        else:
            for file in self.filenames:
                try:
                    self.ThisIsIt(file)
                    doc = rdfDc(file)
                    doc.createUpload(self)
                    jobid = doc.pickle()
                    time.sleep(1)
                    self.statusBar.SetStatusText("Added %s to Upload Queue" % file,0)
                except:
                    logTraceback()
                    print "can't deal with ogg file"
                    self.errDialog("Could not add %s to Upload Queue\nplease check that the file you selected is a valid Ogg Vorbis file.\nThere could be problems with special characters." % file)
                   
            event.Skip()
            
            self.SelectFileButton.SetLabel('File browser')
            self.filenames = []

    def OnWxframe1Paint(self, event):
        event.Skip()

    def OnUpdateProgress(self, event):
        #there are active uploads
        if(self.ul.c > 0):
            self.progressbar.SetValue(int((self.ul.upload_d*100) / self.ul.upload_t))
            self.UploadSpeed.SetLabel('%d  kb/s' % int(self.ul.upload_d / self.ul.c / 1024))
            if(not self.ul.id == ""):
                self.staticText19.SetLabel('%s\ntransferred %d of %d KB' %\
                    (self.ul.id, self.ul.upload_d/1024, self.ul.upload_t/1024))

            list = '' 
            for v in self.ul.inQueue:
                if list == '':
                    list = '\nFiles in Queue:'
                else:
                    list = '%s\n%s' % (list,v)
            self.inQueueText.SetLabel(list)
        else:
            self.UploadSpeed.SetLabel('')
            self.staticText19.SetLabel('%s done' % (self.ul.id))
            self.statusBar.SetStatusText("Finished uploading %s" % self.ul.id,0)
        event.Skip()

    def OnSaveSettingsButton(self, event):
        #first check if new settings are valid
        lists = Lists()
        try:
            list_containers=lists.getContainers(self.username.GetValue())
        except:
            self.errDialog ('Could not connect to server. Please check your Internet Connection;\nMake sure your provided email/password are ok.')
            return

        #new values are ok, save them and update variables
        #save settings to file
        FILE = open('setting.pickle','w')
        cPickle.dump((self.servername.GetValue(), self.username.GetValue(),
                      self.password.GetValue()),FILE)
        FILE.close()
        #update variables, reset stuff if needed
        #advice to restart rut for now.
        self.User = self.username.GetValue()        
        self.dc__type.Destroy()
        self.dc__type = wxComboBox(choices=list_containers, id=wxID_WXFRAME1DC__TYPE,
                name='dc__type', parent=self.panel2, pos=wxPoint(192, 88),
                size=wxSize(192, 22), style=wxCB_READONLY, validator=wxDefaultValidator,
                value='')


        if(self.nothread>0):
            self.wxEVT_UPDATE_PROGRESS = wxEVT_UPDATE_PROGRESS
            self.ul = upload(self)
            thread.start_new_thread(self.ul.startque,())
            self.nothread=1
        self.statusBar.SetStatusText("Settings saved.",0)
#        self.errInfo("You have to restart Reboot Upload Tool to activate the new settings.")

    def OnSelectFileButton(self, event):
        self.filenames = []
        cwd = os.getcwd()
        dlg = wxFileDialog(self, "Choose a file", ".", "", "Ogg Files|*.ogg", wxOPEN|wxMULTIPLE)
        try:
            if dlg.ShowModal() == wxID_OK:
                self.filenames = dlg.GetPaths()
                # Your code
                #print self.filenames
                if len(self.filenames) > 1:
                   self.SelectFileButton.SetLabel("%s files selected" % len(self.filenames))
                   self.statusBar.SetStatusText("%s files selected: %s,.."%(len(self.filenames),self.filenames[0]),0)
                else:
                   self.SelectFileButton.SetLabel(basename(self.filenames[0]))
                   self.statusBar.SetStatusText("Selected %s"%self.filenames[0],0)
                os.chdir(cwd)
        finally:
            dlg.Destroy()

        event.Skip()
