'Main processing/distribution method'

def launchObok(self):

    self.count = 0
    self.books_to_add = []
    self.formats_to_add = []
    self.add_books_cancelled = False
    self.decryption_errors = []
    self.userkeys = []
    self.duplicate_book_list = []
    self.no_home_for_book = []
    self.ids_of_new_books = []
    self.successful_format_adds = []
    self.add_formats_cancelled = False
    self.tdir = PersistentTemporaryDirectory(u'_obok', prefix=u'')
    self.db = self.gui.current_db.new_api
    self.current_idx = self.gui.library_view.currentIndex()
    print(u'Running    {}'.format(((PLUGIN_NAME + u'    v') + PLUGIN_VERSION)))
    tmpserials = cfg[u'kobo_serials']
    device_path = None
    try:
        device = self.parent().device_manager.connected_device
        if device:
            device_path = device._main_prefix
            debug_print(u'get_device_settings    -    device_path=', device_path)
        else:
            debug_print(u"didn't    find    device")
    except:
        debug_print(u'Exception    getting    device    path.    Probably    not    an    E-Ink    Kobo    device')
    self.library = KoboLibrary(tmpserials, device_path)
    debug_print((u'got    kobodir    %s' % self.library.kobodir))
    if (self.library.kobodir == u''):
        msg = _(u'<p>Could    not    find    Kobo    Library\n<p>Windows/Mac:    do    you    have    Kobo    Desktop    installed?\n<p>Windows/Mac/Linux:    In    case    you    have    an    Kobo    eInk    device,    connect    the    device.')
        showErrorDlg(msg, None)
        return
    books = self.build_book_list()
    if (len(books) < 1):
        msg = _(u"<p>No    books    found    in    Kobo    Library\nAre    you    sure    it's    installed\\configured\\synchronized?")
        showErrorDlg(msg, None)
        return
    legacy_key = legacy_obok().get_legacy_cookie_id
    if (legacy_key is not None):
        print(_(u'Legacy    key    found:    '), legacy_key.encode(u'hex_codec'))
        self.userkeys.append(legacy_key)
    try:
        candidate_keys = self.library.userkeys
    except:
        print(_(u'Trouble    retrieving    keys    with    newer    obok    method.'))
        traceback.print_exc()
    else:
        if len(candidate_keys):
            self.userkeys.extend(candidate_keys)
            print(_(u'Found    {0}    possible    keys    to    try.').format(len(self.userkeys)))
    if (not len(self.userkeys)):
        msg = _(u'<p>No    userkeys    found    to    decrypt    books    with.    No    point    in    proceeding.')
        showErrorDlg(msg, None)
        return
    dlg = SelectionDialog(self.gui, self, books)
    if dlg.exec_():
        books_to_import = dlg.getBooks()
        self.count = len(books_to_import)
        debug_print((u'InterfacePluginAction::launchObok    -    number    of    books    to    decrypt:    %d' % self.count))
        d = DecryptAddProgressDialog(self.gui, books_to_import, self.get_decrypted_kobo_books, self.library, u'kobo', status_msg_type=u'Kobo    books', action_type=(u'Decrypting', u'Decryption'))
        if d.wasCanceled():
            print(_(u'{}    -    Decryption    canceled    by    user.').format(((PLUGIN_NAME + u'    v') + PLUGIN_VERSION)))
            self.library.close()
            remove_dir(self.tdir)
            return
    else:
        self.library.close()
        remove_dir(self.tdir)
        return
    self.library.close()
    if len(self.books_to_add):
        d = DecryptAddProgressDialog(self.gui, self.books_to_add, self.add_new_books, self.db, u'calibre', status_msg_type=u'new    calibre    books', action_type=(u'Adding', u'Addition'))
        if d.wasCanceled():
            print(_(u'{}    -    "Add    books"    canceled    by    user.').format(((PLUGIN_NAME + u'    v') + PLUGIN_VERSION)))
            self.add_books_cancelled = True
            print(_(u'{}    -    wrapping    up    results.').format(((PLUGIN_NAME + u'    v') + PLUGIN_VERSION)))
            self.wrap_up_results()
            remove_dir(self.tdir)
            return
    if len(self.duplicate_book_list):
        if (cfg[u'finding_homes_for_formats'] == u'Always'):
            self.process_epub_formats()
        elif (cfg[u'finding_homes_for_formats'] == u'Never'):
            self.no_home_for_book.extend([entry[0] for entry in self.duplicate_book_list])
        elif self.ask_about_inserting_epubs():
            self.process_epub_formats()
        else:
            print(_(u'{}    -    User    opted    not    to    try    to    insert    EPUB    formats').format(((PLUGIN_NAME + u'    v') + PLUGIN_VERSION)))
            self.no_home_for_book.extend([entry[0] for entry in self.duplicate_book_list])
    print(_(u'{}    -    wrapping    up    results.').format(((PLUGIN_NAME + u'    v') + PLUGIN_VERSION)))
    self.wrap_up_results()
    remove_dir(self.tdir)
    return
