Source

VPlayer / searchprocessor.py

# -*- coding: utf-8 -*-

from time import sleep
from threading import Thread

from PyQt4 import QtCore

from vplayer.logger import log
from vplayer.search import Search

class SearchProcessor(QtCore.QThread):
    request = ''
    count = 0
    do_search_more = False
    threads = []
    notfound = False
    #def __init__(self, login):
        #QtCore.QThread.__init__(self)
        #self.login = login

    def search_results_handler(self, results):
        if results[0] == 0:
            self.emit(QtCore.SIGNAL("results"), results[1])
        else:
            if results[1]:
                self.emit(QtCore.SIGNAL("addresults"), results[1])
        all_done = True
        sleep(0.01)
        for thread in self.threads:
            if thread.isRunning():
                log.debug('%s is still running, not sending progress 100' % str(thread))
                all_done = False
                break
        if all_done and not self.notfound:
            self.emit( QtCore.SIGNAL("progress"), 100)

    def search_status_handler(self, message):
        if message == "error":
            self.emit( QtCore.SIGNAL("progress"), -1)
            self._kill_all_threads()
        elif message == 'notfound':
            log.debug('Got nothing found from search engine')
            self.notfound = True
            self.emit( QtCore.SIGNAL("progress"), -2)
            self._kill_all_threads()
        elif message == 'nologin':
            self.emit( QtCore.SIGNAL("progress"), -3)
            self._kill_all_threads()
        elif message == 'wronglogin':
            self.emit( QtCore.SIGNAL("progress"), -4)
            self._kill_all_threads()
        else:
            log.debug('Unhandled search engine status message: %s' % message)

    def _kill_all_threads(self):
        for thread in self.threads:
            #print help(thread.disconnect)
            #thread.disconnect('status')
            #thread.disconnect('results')
            if thread.isRunning():
                thread.terminate()
            #    thread.wait()
        self.threads = []

    def stop_search(self):
        self._kill_all_threads()
        self.emit( QtCore.SIGNAL("progress"), 100)

    def search_base(self, request):
        self.emit( QtCore.SIGNAL("progress"), 0)
        self.notfound = False
        self._kill_all_threads()

        self.request = request
        self.count = 0
        searcher = Search(request, 0)
        self.threads.append(searcher)
        QtCore.QObject.connect(searcher, QtCore.SIGNAL('results'),
                               self.search_results_handler)
        QtCore.QObject.connect(searcher, QtCore.SIGNAL('status'),
                               self.search_status_handler)
        searcher.start()

    def search_more(self):
        self.emit( QtCore.SIGNAL("progress"), 0)
        self.count += 1
        searcher = Search(self.request, self.count)
        self.threads.append(searcher)
        QtCore.QObject.connect(searcher, QtCore.SIGNAL('results'),
                               self.search_results_handler)
        QtCore.QObject.connect(searcher, QtCore.SIGNAL('status'),
                               self.search_status_handler)
        searcher.start()