Guest User

Kubuntu fix for Software propetries menu

a guest
Aug 11th, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.05 KB | Software | 0 0
  1. #  Qt 4 based frontend to software-properties
  2. #
  3. #  Copyright (c) 2007 Canonical Ltd.
  4. #
  5. #  Author: Jonathan Riddell <[email protected]>
  6. #
  7. #  This program is free software; you can redistribute it and/or
  8. #  modify it under the terms of the GNU General Public License as
  9. #  published by the Free Software Foundation; either version 2 of the
  10. #  License, or (at your option) any later version.
  11. #
  12. #  This program is distributed in the hope that it will be useful,
  13. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #  GNU General Public License for more details.
  16. #
  17. #  You should have received a copy of the GNU General Public License
  18. #  along with this program; if not, write to the Free Software
  19. #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  20. #  USA
  21.  
  22. from gettext import gettext as _
  23. import os
  24. import threading
  25.  
  26. from softwareproperties.MirrorTest import MirrorTest
  27. from .I18nHelper import *
  28.  
  29. from PyQt6.QtCore import *
  30. from PyQt6.QtGui import *
  31. from PyQt6 import uic
  32.  
  33. from softwareproperties.CountryInformation import CountryInformation
  34.  
  35. class DialogMirror(QDialog):
  36.  
  37.     # Signals
  38.   test_start = pyqtSignal()
  39.   test_end = pyqtSignal('QString')
  40.   report_progress = pyqtSignal(int, int, tuple, tuple)
  41.   report_action = pyqtSignal('QString')
  42.  
  43.   def __init__(self, parent, datadir, distro, custom_mirrors):
  44.     """
  45.    Initialize the dialog that allows to choose a custom or official mirror
  46.    """
  47.     QDialog.__init__(self, parent)
  48.     uic.loadUi("%s/designer/dialog_mirror.ui" % datadir, self)
  49.     self.parent = parent
  50.  
  51.     self.custom_mirrors = custom_mirrors
  52.  
  53.     self.country_info = CountryInformation()
  54.  
  55.     self.button_choose = self.buttonBox.button(QDialogButtonBox.StandardButton.Ok)
  56.     self.button_choose.setEnabled(False)
  57.     self.button_cancel = self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel)
  58.     self.distro = distro
  59.  
  60.     self.treeview.setColumnCount(1)
  61.     self.treeview.setHeaderLabels([_("Mirror")])
  62.  
  63.     translate_widget(self)
  64.     # used to find the corresponding iter of a location
  65.     map_loc = {}
  66.     patriot = None
  67.     """ no custom yet
  68.    # at first add all custom mirrors and a separator
  69.    if len(self.custom_mirrors) > 0:
  70.        for mirror in self.custom_mirrors:
  71.  
  72.            model.append(None, [mirror, False, True, None])
  73.            self.column_mirror.add_attribute(self.renderer_mirror,
  74.                                             "editable",
  75.                                             COLUMN_CUSTOM)
  76.        model.append(None, [None, True, False, None])
  77.    """
  78.     self.mirror_map = {}
  79.     # secondly add all official mirrors
  80.     for hostname in self.distro.source_template.mirror_set.keys():
  81.         mirror = self.distro.source_template.mirror_set[hostname]
  82.         if mirror.location in map_loc:  # a mirror in a country
  83.             QTreeWidgetItem(map_loc[mirror.location], [hostname])
  84.             self.mirror_map[hostname] = mirror
  85.         elif mirror.location != None:  # a country new to the list
  86.             country = self.country_info.get_country_name(mirror.location)
  87.             parent = QTreeWidgetItem([country])
  88.             self.mirror_map[country] = None
  89.             self.treeview.addTopLevelItem(parent)
  90.             QTreeWidgetItem(parent, [hostname])
  91.             self.mirror_map[hostname] = mirror
  92.             if mirror.location == self.country_info.code and patriot == None:
  93.                 patriot = parent
  94.             map_loc[mirror.location] = parent
  95.         else:  # a mirror without country
  96.             item = QTreeWidgetItem([hostname])
  97.             self.treeview.addTopLevelItem(item)
  98.     self.treeview.sortItems(0, Qt.SortOrder.AscendingOrder)
  99.     # Scroll to the local mirror set
  100.     if patriot != None:
  101.         self.select_mirror(patriot.text(0))
  102.     self.treeview.itemClicked.connect(self.on_treeview_mirrors_cursor_changed)
  103.     self.button_find_server.clicked.connect(self.on_button_test_clicked)
  104.     self.edit_buttons_frame.hide()  ##FIXME not yet implemented
  105.  
  106.   def on_treeview_mirrors_cursor_changed(self, item, column):
  107.     """ Check if the currently selected row in the mirror list
  108.        contains a mirror and or is editable """
  109.     # Update the list of available protocolls
  110.     hostname = self.treeview.currentItem().text(0)
  111.     mirror = self.mirror_map[hostname]
  112.     self.combobox.clear()
  113.     if mirror != None:
  114.         self.combobox.setEnabled(True)
  115.         seen_protos = []
  116.         self.protocol_paths = {}
  117.         for repo in mirror.repositories:
  118.             # Only add a repository for a protocoll once
  119.             if repo.proto in seen_protos:
  120.                 continue
  121.             seen_protos.append(repo.proto)
  122.             self.protocol_paths[repo.get_info()[0]] = repo.get_info()[1]
  123.             self.combobox.addItem(repo.get_info()[0])
  124.         self.button_choose.setEnabled(True)
  125.     else:
  126.         self.button_choose.setEnabled(False)
  127.     """
  128.        # Allow to edit and remove custom mirrors
  129.        self.button_remove.set_sensitive(model.get_value(iter, COLUMN_CUSTOM))
  130.        self.button_edit.set_sensitive(model.get_value(iter, COLUMN_CUSTOM))
  131.        self.button_choose.set_sensitive(self.is_valid_mirror(model.get_value(iter, COLUMN_URI)))
  132.        self.combobox.set_sensitive(False)
  133.  
  134.    """
  135.  
  136.   def run(self):
  137.     """ Run the chooser dialog and return the chosen mirror or None """
  138.     res = self.exec()
  139.  
  140.     # FIXME: we should also return the list of custom servers
  141.     if res == QDialog.DialogCode.Accepted:
  142.         hostname = self.treeview.currentItem().text(0)
  143.         mirror = self.mirror_map[hostname]
  144.  
  145.         if mirror == None:
  146.             # Return the URL of the selected custom mirror
  147.             print("Error, unknown mirror")
  148.             return None
  149.             ##FIXME return model.get_value(iter, COLUMN_URI)
  150.         else:
  151.             # Return a URL created from the hostname and the selected
  152.             # repository
  153.             proto = self.combobox.currentText()
  154.  
  155.             directory = self.protocol_paths[proto]
  156.             return "%s://%s/%s" % (proto, mirror.hostname, directory)
  157.     else:
  158.         return None
  159.  
  160.   def on_button_test_clicked(self):
  161.     ''' Perform a test to find the best mirror and select it
  162.        afterwards in the treeview '''
  163.     class MirrorTestQt(MirrorTest):
  164.         def __init__(self, mirrors, test_file, running, dialog, parent):
  165.             MirrorTest.__init__(self, mirrors, test_file, None, running)
  166.             self.dialog = dialog
  167.             self.parent = parent
  168.  
  169.         def report_action(self, text):
  170.             if self.running.isSet():
  171.                 self.parent.report_action.emit(text)
  172.  
  173.         def report_progress(self, current, max, borders=(0,1), mod=(0,0)):
  174.             if self.running.isSet():
  175.                 self.parent.report_progress.emit(current, max, borders, mod)
  176.  
  177.         def run(self):
  178.             self.parent.test_start.emit()
  179.             rocker = self.run_full_test()
  180.             self.parent.test_end.emit(rocker)
  181.  
  182.     self.dialog = QProgressDialog(_("Testing Mirrors"), _("Cancel"), 0, 100, self)
  183.     self.dialog.setWindowTitle(_("Testing Mirrors"))
  184.     self.dialog.setWindowModality(Qt.WindowModality.WindowModal)
  185.     self.button_cancel_test = QPushButton(_("Cancel"), self.dialog)
  186.     self.dialog.setCancelButton(self.button_cancel_test)
  187.     self.button_cancel_test.clicked.connect(self.on_button_cancel_test_clicked);
  188.     # the following signals are connected across threads
  189.     self.test_start.connect(self.on_test_start, Qt.ConnectionType.BlockingQueuedConnection)
  190.     self.test_end.connect(self.on_test_end, Qt.ConnectionType.BlockingQueuedConnection)
  191.     self.report_progress.connect(self.on_report_progress, Qt.ConnectionType.BlockingQueuedConnection)
  192.     self.report_action.connect(self.on_report_action, Qt.ConnectionType.BlockingQueuedConnection)
  193.  
  194.     self.running = threading.Event()
  195.     self.running.set()
  196.     pipe = os.popen("dpkg --print-architecture")
  197.     arch = pipe.read().strip()
  198.     test_file = "dists/%s/%s/binary-%s/Packages.gz" % \
  199.                  (self.distro.source_template.name,
  200.                   self.distro.source_template.components[0].name,
  201.                   arch)
  202.     test = MirrorTestQt(list(self.distro.source_template.mirror_set.values()),
  203.                          test_file, self.running, self.dialog, self)
  204.     test.start() # test starts in a separate thread
  205.  
  206.   def on_test_start(self):
  207.       self.dialog.show()
  208.  
  209.   def on_test_end(self, mirror):
  210.       self.dialog.hide()
  211.       if not self.running.isSet():
  212.           return # canceled by user
  213.       if mirror != None:
  214.           self.select_mirror(mirror)
  215.       else:
  216.           QMessageBox.warning(self.dialog, _("Testing Mirrors"),
  217.                                   _("No suitable download server was found") + "\n" +
  218.                                   _("Please check your Internet connection."))
  219.  
  220.   def on_report_action(self, text):
  221.       self.dialog.setLabelText(str("<i>%s</i>" % text))
  222.  
  223.   def on_report_progress(self, current, max, borders=(0,1), mod=(0,0)):
  224.       #self.dialog.setLabelText(_("Completed %s of %s tests") % \
  225.       #                       (current + mod[0], max + mod[1]))
  226.       frac = borders[0] + (borders[1] - borders[0]) / max * current
  227.       self.dialog.setValue(int(frac*100))
  228.  
  229.   def on_button_cancel_test_clicked(self):
  230.     ''' Abort the mirror performance test '''
  231.     self.running.clear()
  232.     self.dialog.show()
  233.     self.dialog.setLabelText("<i>%s</i>" % _("Canceling..."))
  234.     self.button_cancel_test.setEnabled(False)
  235.     self.dialog.setValue(100)
  236.  
  237.   def select_mirror(self, mirror):
  238.     """Select and expand the path to a matching mirror in the list"""
  239.     found = self.treeview.findItems(mirror,Qt.MatchFlag.MatchExactly|Qt.MatchFlag.MatchRecursive)
  240.     if found:
  241.       found[0].setSelected(True)
  242.       self.treeview.setCurrentItem(found[0])
  243.       self.treeview.scrollToItem(found[0], QAbstractItemView.ScrollHint.PositionAtCenter)
  244.       self.on_treeview_mirrors_cursor_changed(found[0], 0)
  245.       self.button_choose.setFocus()
  246.       return True
Advertisement
Add Comment
Please, Sign In to add comment