Example #1
0
def main():
    if not os.getenv('PYTHONUNBUFFERED'):
        os.environ['PYTHONUNBUFFERED'] = '1'

    urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

    args = app_args.read()

    logger = logs.new_logger(__app_name__, bool(args.logs))
    app_args.validate(args, logger)

    local_config = config.read_config(update_file=True)

    i18n_key, current_i18n = translation.get_locale_keys(
        local_config['locale'])
    default_i18n = translation.get_locale_keys(
        DEFAULT_I18N_KEY)[1] if i18n_key != DEFAULT_I18N_KEY else {}
    i18n = I18n(i18n_key, current_i18n, DEFAULT_I18N_KEY, default_i18n)

    cache_cleaner = CacheCleaner()
    cache_factory = DefaultMemoryCacheFactory(expiration_time=int(
        local_config['memory_cache']['data_expiration']),
                                              cleaner=cache_cleaner)
    icon_cache = cache_factory.new(
        int(local_config['memory_cache']['icon_expiration']))

    http_client = HttpClient(logger)

    context = ApplicationContext(
        i18n=i18n,
        http_client=http_client,
        disk_cache=bool(local_config['disk_cache']['enabled']),
        download_icons=bool(local_config['download']['icons']),
        app_root_dir=ROOT_DIR,
        cache_factory=cache_factory,
        disk_loader_factory=DefaultDiskCacheLoaderFactory(
            disk_cache_enabled=bool(local_config['disk_cache']['enabled']),
            logger=logger),
        logger=logger,
        distro=util.get_distro(),
        file_downloader=AdaptableFileDownloader(
            logger, bool(local_config['download']['multithreaded']), i18n,
            http_client))

    managers = gems.load_managers(context=context,
                                  locale=i18n_key,
                                  config=local_config,
                                  default_locale=DEFAULT_I18N_KEY)

    if args.reset:
        util.clean_app_files(managers)
        exit(0)

    manager = GenericSoftwareManager(managers,
                                     context=context,
                                     config=local_config)
    manager.prepare()

    app = QApplication(sys.argv)
    app.setApplicationName(__app_name__)
    app.setApplicationVersion(__version__)
    app_icon = util.get_default_icon()[1]
    app.setWindowIcon(app_icon)

    if local_config['ui']['style']:
        app.setStyle(str(local_config['ui']['style']))
    else:
        if app.style().objectName().lower() not in {'fusion', 'breeze'}:
            app.setStyle('Fusion')

    manage_window = ManageWindow(i18n=i18n,
                                 manager=manager,
                                 icon_cache=icon_cache,
                                 screen_size=app.primaryScreen().size(),
                                 config=local_config,
                                 context=context,
                                 http_client=http_client,
                                 icon=app_icon,
                                 logger=logger)

    if args.tray:
        tray_icon = TrayIcon(i18n=i18n,
                             manager=manager,
                             manage_window=manage_window,
                             config=local_config)
        manage_window.set_tray_icon(tray_icon)
        tray_icon.show()

        if args.show_panel:
            tray_icon.show_manage_window()
    else:
        manage_window.refresh_apps()
        manage_window.show()

    cache_cleaner.start()
    Thread(target=config.remove_old_config, args=(logger, ),
           daemon=True).start()
    sys.exit(app.exec_())
Example #2
0
    def run(self):
        valid_password, root_password = self._request_password()

        if not valid_password:
            self.notify_finished({
                'success': False,
                'updated': 0,
                'types': set(),
                'id': None
            })
            self.pkgs = None
            return

        to_update = [
            pkg for pkg in self.pkgs if pkg.model.update
            and not pkg.model.is_update_ignored() and pkg.update_checked
        ]

        if len(to_update) > 1:
            self.disable_progress_controll()
        else:
            self.enable_progress_controll()

        success = False

        updated, updated_types = 0, set()

        models = [view.model for view in to_update]

        self.change_substatus(self.i18n['action.update.requirements.status'])
        requirements = self.manager.get_upgrade_requirements(
            models, root_password, self)

        if not requirements:
            self.pkgs = None
            self.notify_finished({
                'success': success,
                'updated': updated,
                'types': updated_types,
                'id': None
            })
            return

        comps, required_size, extra_size = [], 0, 0

        if requirements.cannot_upgrade:
            comps.append(
                self._gen_cannot_update_form(requirements.cannot_upgrade))

        if requirements.to_install:
            req_form, reqs_size = self._gen_to_install_form(
                requirements.to_install)
            required_size += reqs_size[0]
            extra_size += reqs_size[1]
            comps.append(req_form)

        if requirements.to_remove:
            comps.append(self._gen_to_remove_form(requirements.to_remove))

        updates_form, updates_size = self._gen_to_update_form(
            requirements.to_upgrade)
        required_size += updates_size[0]
        extra_size += updates_size[1]
        comps.append(updates_form)

        extra_size_text = '{}: {}'.format(
            self.i18n['action.update.total_size'].capitalize(),
            get_human_size_str(extra_size))
        req_size_text = '{}: {}'.format(
            self.i18n['action.update.required_size'].capitalize(),
            get_human_size_str(required_size))
        comps.insert(
            0,
            TextComponent('{}  |  {}'.format(extra_size_text, req_size_text),
                          size=14))
        comps.insert(1, TextComponent(''))

        if not self.request_confirmation(
                title=self.i18n['action.update.summary'].capitalize(),
                body='',
                components=comps,
                confirmation_label=self.i18n['proceed'].capitalize(),
                deny_label=self.i18n['cancel'].capitalize()):
            self.notify_finished({
                'success': success,
                'updated': updated,
                'types': updated_types,
                'id': None
            })
            self.pkgs = None
            return

        self.change_substatus('')

        app_config = read_config()

        # trim dialog
        if app_config['disk']['trim']['after_upgrade'] is not False:
            should_trim = app_config['disk']['trim'][
                'after_upgrade'] or self._ask_for_trim()
        else:
            should_trim = False

        # backup process ( if enabled, supported and accepted )
        if bool(app_config['backup']
                ['enabled']) and app_config['backup']['upgrade'] in (
                    True, None) and timeshift.is_available():
            any_requires_bkp = False

            for dep in requirements.to_upgrade:
                if dep.pkg.supports_backup():
                    any_requires_bkp = True
                    break

            if any_requires_bkp:
                if not self.request_backup(app_config, 'upgrade', self.i18n,
                                           root_password):
                    self.notify_finished({
                        'success': success,
                        'updated': updated,
                        'types': updated_types,
                        'id': None
                    })
                    self.pkgs = None
                    return

        self.change_substatus('')

        timestamp = datetime.now()
        upgrade_id = 'upgrade_{}{}{}_{}'.format(timestamp.year,
                                                timestamp.month, timestamp.day,
                                                int(time.time()))

        self._write_summary_log(upgrade_id, requirements)

        success = bool(self.manager.upgrade(requirements, root_password, self))
        self.change_substatus('')

        if success:
            updated = len(requirements.to_upgrade)
            updated_types.update(
                (req.pkg.__class__ for req in requirements.to_upgrade))

            if should_trim:
                self._trim_disk(root_password)

            if bool(app_config['updates']['ask_for_reboot']):
                msg = '<p>{}</p>{}</p><br/><p>{}</p>'.format(
                    self.i18n['action.update.success.reboot.line1'],
                    self.i18n['action.update.success.reboot.line2'],
                    self.i18n['action.update.success.reboot.line3'])
                self.request_reboot(msg)

        self.notify_finished({
            'success': success,
            'updated': updated,
            'types': updated_types,
            'id': upgrade_id
        })
        self.pkgs = None
Example #3
0
    def get_settings(self, screen_width: int,
                     screen_height: int) -> ViewComponent:
        tabs = list()

        gem_opts, def_gem_opts, gem_tabs = [], set(), []

        for man in self.managers:
            if man.can_work():
                man_comp = man.get_settings(screen_width, screen_height)
                modname = man.__module__.split('.')[-2]
                icon_path = "{r}/gems/{n}/resources/img/{n}.svg".format(
                    r=ROOT_DIR, n=modname)

                if man_comp:
                    tab_name = self.i18n.get('gem.{}.label'.format(modname),
                                             modname.capitalize())
                    gem_tabs.append(
                        TabComponent(label=tab_name,
                                     content=man_comp,
                                     icon_path=icon_path,
                                     id_=modname))

                opt = InputOption(
                    label=self.i18n.get('gem.{}.label'.format(modname),
                                        modname.capitalize()),
                    tooltip=self.i18n.get('gem.{}.info'.format(modname)),
                    value=modname,
                    icon_path='{r}/gems/{n}/resources/img/{n}.svg'.format(
                        r=ROOT_DIR, n=modname))
                gem_opts.append(opt)

                if man.is_enabled() and man in self.working_managers:
                    def_gem_opts.add(opt)

        core_config = read_config()

        if gem_opts:
            type_help = TextComponent(html=self.i18n['core.config.types.tip'])
            gem_opts.sort(key=lambda o: o.value)
            gem_selector = MultipleSelectComponent(
                label=None,
                tooltip=None,
                options=gem_opts,
                max_width=floor(screen_width * 0.22),
                default_options=def_gem_opts,
                id_="gems")
            tabs.append(
                TabComponent(label=self.i18n['core.config.tab.types'],
                             content=PanelComponent([
                                 type_help,
                                 FormComponent([gem_selector], spaces=False)
                             ]),
                             id_='core.types'))

        tabs.append(
            self._gen_general_settings(core_config, screen_width,
                                       screen_height))
        tabs.append(
            self._gen_ui_settings(core_config, screen_width, screen_height))
        tabs.append(
            self._gen_tray_settings(core_config, screen_width, screen_height))
        tabs.append(
            self._gen_adv_settings(core_config, screen_width, screen_height))

        bkp_settings = self._gen_backup_settings(core_config, screen_width,
                                                 screen_height)

        if bkp_settings:
            tabs.append(bkp_settings)

        for tab in gem_tabs:
            tabs.append(tab)

        return TabGroupComponent(tabs)
Example #4
0
    def _save_settings(self, general: PanelComponent, advanced: PanelComponent,
                       backup: PanelComponent, ui: PanelComponent,
                       tray: PanelComponent,
                       gems_panel: PanelComponent) -> Tuple[bool, List[str]]:
        core_config = config.read_config()

        # general
        general_form = general.components[0]

        locale = general_form.get_component('locale').get_selected()

        if locale != self.i18n.current_key:
            core_config['locale'] = locale

        core_config['system']['notifications'] = general_form.get_component(
            'sys_notify').get_selected()
        core_config['suggestions']['enabled'] = general_form.get_component(
            'sugs_enabled').get_selected()
        core_config['store_root_password'] = general_form.get_component(
            'store_pwd').get_selected()

        sugs_by_type = general_form.get_component(
            'sugs_by_type').get_int_value()
        core_config['suggestions']['by_type'] = sugs_by_type

        core_config['updates']['ask_for_reboot'] = general_form.get_component(
            'ask_for_reboot').get_selected()

        # advanced
        adv_form = advanced.components[0]

        download_mthreaded = adv_form.get_component(
            'down_mthread').get_selected()
        core_config['download']['multithreaded'] = download_mthreaded

        mthread_client = adv_form.get_component(
            'mthread_client').get_selected()
        core_config['download']['multithreaded_client'] = mthread_client

        if isinstance(self.file_downloader, AdaptableFileDownloader):
            self.file_downloader.multithread_client = mthread_client
            self.file_downloader.multithread_enabled = download_mthreaded

        single_dep_check = adv_form.get_component('dep_check').get_selected()
        core_config['system']['single_dependency_checking'] = single_dep_check

        data_exp = adv_form.get_component('data_exp').get_int_value()
        core_config['memory_cache']['data_expiration'] = data_exp

        icon_exp = adv_form.get_component('icon_exp').get_int_value()
        core_config['memory_cache']['icon_expiration'] = icon_exp

        core_config['disk']['trim']['after_upgrade'] = adv_form.get_component(
            'trim_after_upgrade').get_selected()

        # backup
        if backup:
            bkp_form = backup.components[0]

            core_config['backup']['enabled'] = bkp_form.get_component(
                'enabled').get_selected()
            core_config['backup']['mode'] = bkp_form.get_component(
                'mode').get_selected()
            core_config['backup']['type'] = bkp_form.get_component(
                'type').get_selected()
            core_config['backup']['install'] = bkp_form.get_component(
                'install').get_selected()
            core_config['backup']['uninstall'] = bkp_form.get_component(
                'uninstall').get_selected()
            core_config['backup']['upgrade'] = bkp_form.get_component(
                'upgrade').get_selected()
            core_config['backup']['downgrade'] = bkp_form.get_component(
                'downgrade').get_selected()

        # tray
        tray_form = tray.components[0]
        core_config['updates']['check_interval'] = tray_form.get_component(
            'updates_interval').get_int_value()

        def_icon_path = tray_form.get_component('def_icon').file_path
        core_config['ui']['tray'][
            'default_icon'] = def_icon_path if def_icon_path else None

        up_icon_path = tray_form.get_component('up_icon').file_path
        core_config['ui']['tray'][
            'updates_icon'] = up_icon_path if up_icon_path else None

        # ui
        ui_form = ui.components[0]

        core_config['download']['icons'] = ui_form.get_component(
            'down_icons').get_selected()
        core_config['ui']['hdpi'] = ui_form.get_component(
            'hdpi').get_selected()

        previous_autoscale = core_config['ui']['auto_scale']

        core_config['ui']['auto_scale'] = ui_form.get_component(
            'auto_scale').get_selected()

        if previous_autoscale and not core_config['ui']['auto_scale']:
            self.logger.info(
                "Deleting environment variable QT_AUTO_SCREEN_SCALE_FACTOR")
            del os.environ['QT_AUTO_SCREEN_SCALE_FACTOR']

        core_config['ui']['scale_factor'] = ui_form.get_component(
            'scalef').value / 100
        core_config['ui']['table']['max_displayed'] = ui_form.get_component(
            'table_max').get_int_value()

        style = ui_form.get_component('style').get_selected()

        cur_style = core_config['ui']['style'] if core_config['ui'][
            'style'] else QApplication.instance().style().objectName().lower()
        if style != cur_style:
            core_config['ui']['style'] = style

        # gems
        checked_gems = gems_panel.components[1].get_component(
            'gems').get_selected_values()

        for man in self.managers:
            modname = man.__module__.split('.')[-2]
            enabled = modname in checked_gems
            man.set_enabled(enabled)

        core_config['gems'] = None if core_config['gems'] is None and len(
            checked_gems) == len(self.managers) else checked_gems

        try:
            config.save(core_config)
            return True, None
        except:
            return False, [traceback.format_exc()]
Example #5
0
def ask_root_password(
        context: ApplicationContext,
        i18n: I18n,
        app_config: dict = None,
        comp_manager: QtComponentsManager = None) -> Tuple[str, bool]:

    cur_config = read_config() if not app_config else app_config
    store_password = bool(cur_config['store_root_password'])

    if store_password and context.root_password and validate_password(
            context.root_password):
        return context.root_password, True

    diag = QInputDialog(flags=Qt.CustomizeWindowHint | Qt.WindowTitleHint)
    diag.setStyleSheet(
        """QLineEdit {  border-radius: 5px; font-size: 16px; border: 1px solid lightblue }"""
    )
    diag.setInputMode(QInputDialog.TextInput)
    diag.setTextEchoMode(QLineEdit.Password)
    diag.setWindowIcon(util.get_default_icon()[1])
    diag.setWindowTitle(i18n['popup.root.title'])
    diag.setLabelText('')
    diag.setOkButtonText(i18n['popup.root.continue'].capitalize())
    diag.setCancelButtonText(i18n['popup.button.cancel'].capitalize())

    bt_box = [c for c in diag.children() if isinstance(c, QDialogButtonBox)][0]

    for bt in bt_box.buttons():
        if bt_box.buttonRole(bt) == QDialogButtonBox.AcceptRole:
            bt.setStyleSheet(css.OK_BUTTON)

        bt.setCursor(QCursor(Qt.PointingHandCursor))
        bt.setIcon(QIcon())

    diag.resize(400, 200)

    if comp_manager:
        comp_manager.save_states(state_id=ACTION_ASK_ROOT, only_visible=True)
        comp_manager.disable_visible()

    for attempt in range(3):

        ok = diag.exec_()

        if ok:
            QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
            valid = validate_password(diag.textValue())
            QApplication.restoreOverrideCursor()

            if not valid:
                body = i18n['popup.root.bad_password.body']

                if attempt == 2:
                    body += '. ' + i18n['popup.root.bad_password.last_try']

                show_message(title=i18n['popup.root.bad_password.title'],
                             body=body,
                             type_=MessageType.ERROR)
                ok = False
                diag.setTextValue('')

            if ok:
                if store_password:
                    context.root_password = diag.textValue()

                if comp_manager:
                    comp_manager.restore_state(ACTION_ASK_ROOT)

                return diag.textValue(), ok
        else:
            break

    if comp_manager:
        comp_manager.restore_state(ACTION_ASK_ROOT)

    return '', False