def sendEmail(self):
        if not self.sending:
            self.sending = True
        else:
            return
        print 'sendEmail',self.imagecount
        imglist = []
        if self.imagecount == 1:
            if not self.gwidget == 0:
                self.gwidget.saveText()
                imglist.append(self.gwidget.getImageFile())
                #dpi = self.gwidget.dpi

        else:
            #multiple
            if self.imagecount == 0:
                return
            print self.workingtab.count()
            for c in range(0,self.workingtab.count()):
                print 'getting multiple images',c
                self.workingtab.widget(c).saveText()
                imglist.append(self.workingtab.widget(c).getImageFile())
                #dpi = self.workingtab.widget(c).dpi

        print imglist
        print str(config.getDestFolder())+'/pdf/spool/'+str(self.identifier)+'.pdf'
        pdfgen.createPDF(str(config.getDestFolder())+'/pdf/spool/'+str(self.identifier)+'.pdf',imglist)
    def removeCurrentImage(self):
        print 'removeCurrentImage imagecount',self.imagecount
        if self.imagecount == 1:
            self.gwidget.deleteFile()
            os.rmdir(str(config.getDestFolder())+'/scans/'+self.identifier)
            return 0

        elif self.imagecount == 0:
            os.rmdir(str(config.getDestFolder())+'/scans/'+self.identifier)
            return 0

        else:
            self.workingtab.currentWidget().deleteFile()
            self.workingtab.removeTab(self.workingtab.currentIndex())
            self.imagecount -= 1
            return 1
    def __init__(self,identifier = None):
        super(scanPage, self).__init__()

        writepath = config.getDestFolder()
        emaildest = config.getDestEmail()
        self.gwidget = 0
        self.workingtab = 0
  #      self.createActions()
        self.layout = QtGui.QVBoxLayout()
#        self.toolbar = QtGui.QToolBar('Tools')
 #       self.toolbar.addAction(self.savefile)
#        self.toolbar.addAction(self.sendemail)
 #       self.layout.addWidget(self.toolbar)
        self.setLayout(self.layout)
        self.imagecount = 0
        if identifier == None:
            self.identifier = config.timeStamp()
        else:
            self.identifier = identifier
        self.sending = False
        if not os.path.exists(str(config.getDestFolder())+'/scans/'+self.identifier):
            print 'path does not exist', str(config.getDestFolder())+'/scans/'+self.identifier
            os.mkdir(str(config.getDestFolder())+'/scans/'+self.identifier)
    def run(self):
        while self.mailercontinue:
            if len(os.listdir(str(config.getDestFolder())+'/pdf/spool')) > 0:
                filename = os.listdir(str(config.getDestFolder())+'/pdf/spool')[0]
                filepath = str(config.getDestFolder())+'/pdf/spool/'+ filename
                try:
                    smtp = Smtp(str(config.getSMTPServer()), str(config.getSMTPUser()), str(config.getSMTPPassword()),587)
                except:
                    self.mailercontinue = False
                    print 'error logging in'
                    self.terminate()

                smtp.subject(filename+' attached')
                smtp.message('Book keeping data attached.')
                smtp.from_addr(str(config.getSMTPUser()))
                print 'sending email to',str(config.getDestEmail())
                smtp.rcpt_to(str(config.getDestEmail()))

                smtp.attach(filepath)
                try:
                    smtp.send()
                    #move file
                
                except:
                    print 'send failed'
                    break
                
                os.rename(filepath,str(config.getDestFolder())+'/pdf/save/'+filename)
                print 'sending completed signal'
                self.emit(QtCore.SIGNAL('fileSent(QString)'),os.path.splitext(filename)[0])
                
                self.sleep(2)

            else:
                #print 'thread sleeping'
                self.sleep(10)
 def saveImageFiles(self):
     os.rename(str(config.getDestFolder())+'/scans/'+self.identifier,str(config.getDestFolder())+'/imagesave/'+self.identifier)