def _create_window(self, x=None, y=None, width=None, height=None, text=None): hmenu = NULL lpparam = NULL hwnd = self.parent and self.parent._hwnd or NULL frontend_hinstance = self.frontend._hinstance if x is None: x = CW_USEDEFAULT if y is None: y = CW_USEDEFAULT if width is None: width = CW_USEDEFAULT if height is None: height = CW_USEDEFAULT if not text: text = "" self._hwnd = CreateWindowEx(self._window_ex_style_, text_type(self._window_class_name_), text_type(text), self._window_style_, x, y, width, height, hwnd, hmenu, frontend_hinstance, lpparam)
def ask_to_retry(self, message, title=None): if not title: title = self.get_title() result = windll.user32.MessageBoxW(self.main_window._hwnd, text_type(message), text_type(title), MB_RETRYCANCEL) return result == IDRETRY
def ask_confirmation(self, message, title=None): if not title: title = self.get_title() result = windll.user32.MessageBoxW(self.main_window._hwnd, text_type(message), text_type(title), MB_YESNO | MB_ICONQUESTION) return result == IDYES
def install(self): source = misc.text_type(self.maindialog.sources.get_selection()) target = misc.text_type(self.maindialog.targets.get_selection()) if self.maindialog.persist_radio.is_checked(): persist = self.maindialog.persist_slider.get_pos() else: persist = 0 if source and target: self.installwindow.show() self.main_window.hide() self.backend.install(source, target, persist)
def __init__(self, backend, img=None, persistent=True, allow_system_internal=False): QObject.__init__(self) #our passed vars - keep them private self.__persistent = persistent self.__img = img self.__allow_system_internal = allow_system_internal # Perform some initialization self.__initPrivateVars() self.__initUI() #enable the backend self.__backend = backend # Connect to backend signals. self.__backend.source_added_cb = self.add_source self.__backend.target_added_cb = self.add_target self.__backend.source_removed_cb = self.remove_source self.__backend.target_removed_cb = self.remove_target self.__backend.failure_cb = self.failure self.__backend.success_cb = self.success self.__backend.install_progress_cb = self.progress self.__backend.install_progress_message_cb = self.progress_message self.__backend.retry_cb = self.retry self.__backend.target_changed_cb = self.update_target self.__backend.format_ended_cb = self.format_ended self.__backend.format_failed_cb = self.format_failed #add any file sources passed if self.__img is not None: self.__backend.add_image(misc.text_type(self.__img)) downloadsDir = QDir(KGlobalSettings.self().downloadPath()) isoFilter = [] isoFilter.append("*.iso") for iso in downloadsDir.entryList(isoFilter, QDir.Files): self.__backend.add_image(misc.text_type(downloadsDir.absoluteFilePath(iso))) def test_func(*a): if not queue.empty(): func = queue.get_nowait() func() queue.task_done() return True self.queue_processor = self.add_timeout(500, test_func, None) self.__backend.detect_devices() self.update_loop = self.add_timeout(2000, self.__backend.update_free)
def add_target(self, target): logging.debug('add_target: %s' % misc.text_type(target)) new_item = QTreeWidgetItem(self.__mainWindow.ui_dest_list) new_item.setData(0,Qt.UserRole,target) # FIXME: # the new_item lines should be auto triggered onChange to the # TreeWidget when new_item is appended. new_item.setText(0,target) new_item.setIcon(0,KIcon("drive-removable-media-usb-pendrive")) item = self.__mainWindow.ui_dest_list.currentItem() if not item: item = self.__mainWindow.ui_dest_list.topLevelItem(0) if item: self.__mainWindow.ui_dest_list.setCurrentItem(item,True) # populate from device data if self.__backend is not None: dev = self.__backend.targets[target] pretty_name = "%s %s (%s)" % (dev['vendor'], dev['model'], dev['device']) new_item.setText(0,pretty_name) new_item.setText(1,dev['label']) new_item.setText(2,misc.format_size(dev['capacity'])) free = dev['free'] if free >= 0: new_item.setText(3,misc.format_size(free)) else: new_item.setText(3,'')
def set_item(self, idx, subidx, text): item = LVITEM() item.mask = LVIF_TEXT item.iItem = idx item.iSubItem = subidx item.pszText = text_type(text) self._send_message(LVM_SETITEM, 0, byref(item))
def __init__(self, parent=None, x=None, y=None, width=None, height=None, text=None, frontend=None, icon=None): self.parent = parent self._icon = None if frontend: self.frontend = frontend else: self.frontend = parent.frontend if not self.__class__._window_class_name_: self.__class__._window_class_name_ = self.__class__.__name__ if icon: self._icon = windll.user32.LoadImageW(NULL, text_type(icon), IMAGE_ICON, 0, 0, LR_LOADFROMFILE) self._register_window() self._create_window(x, y, width, height, text) self._register_handlers() self.on_init()
def set_text(self, text): old_text = self.get_text() if not windll.user32.SetWindowTextW(self._hwnd, text_type(text)): raise WinError() if old_text and old_text.rstrip() != text.rstrip(): # without update, text is displayed on top of old text when background is transparent # TBD check _on_ctlcolorstatic whether that can be avoided self.update(full=True)
def add_item(self, title, child, position=0): item = TCITEM() item.mask = TCIF_TEXT | TCIF_PARAM item.pszText = text_type(title) item.lParam = child._hwnd #~ self.InsertItem(index, item) #~ self._ResizeChild(child) #~ self.SetCurrentTab(index) self._send_message(TCM_INSERTITEM, position, byref(item))
def get_target(self): '''Returns the UDI of the selected target disk or partition.''' item = self.__mainWindow.ui_dest_list.currentItem() if item: # Must deal in unicode and not QString for backend dest = misc.text_type(item.data(0,Qt.UserRole)) return dest else: logging.debug('No target selected.') return ''
def get_source(self): '''Returns the UDI of the selected source image.''' item = self.__mainWindow.ui_source_list.currentItem() if item: # Must deal in unicode and not QString for backend source = misc.text_type(item.data(0,Qt.UserRole)) return source else: logging.debug('No source selected.') return ''
def insert_item(self, item_name, idx): if idx < 0: # Append. idx = self._send_message(LVM_GETITEMCOUNT, 0, 0) item = LVITEM() item.mask = LVIF_TEXT item.iItem = idx item.iSubItem = 0 item.pszText = text_type(item_name) return self._send_message(LVM_INSERTITEM, 0, byref(item))
def add_file_source_dialog(self): filename = '' filter = '*.iso|' + _('CD Images') + '\n*.img|' + _('Disk Images') # FIXME: should set the default path KUrl to users home dir... # This is all screwy as its run as root under kdesudo... Home = root and not user.. blarg! # Need to convert to plain string for backend to work filename = misc.text_type(KFileDialog.getOpenFileName(KUrl(),filter)) if not filename: return self.__backend.add_image(filename)
def source_selection_changed(self, current_item, prev_item): '''The selected image has changed we need to refresh targets''' if not self.__backend: return if current_item is not None: udi = misc.text_type(current_item.data(0,Qt.UserRole)) else: udi = None self.__backend.set_current_source(udi) item = self.__mainWindow.ui_dest_list.currentItem() self.dest_selection_changed(item, None)
def dest_selection_changed(self, current_item, prev_item): '''The selected partition has changed and the bounds on the persistence slider need to be changed, or the slider needs to be disabled, to reflect the amount of free space on the partition.''' if not self.__backend: return if current_item is None: return udi = misc.text_type(current_item.data(0,Qt.UserRole)) self.update_target(udi)
def _add_drive(self, device): logging.debug('disk added: %s' % device) dk = self.bus.get_object(DISKS_IFACE, device) def get(prop): return dk.Get(device, prop, dbus_interface=PROPS_IFACE) model = get('DriveModel') vendor = get('DriveVendor') device_file = get('device-file') size = get('device-size') if size > 0: self.targets[misc.text_type(device)] = { 'vendor': vendor, 'model': model, 'label': '', 'free': -1, 'device': misc.text_type(device_file), 'capacity': size, 'status': misc.NEED_FORMAT, 'mountpoint': None, 'persist': 0, 'parent': None, 'formatting': False, } if misc.callable(self.target_added_cb): if self.show_all: self.target_added_cb(device) else: children = [ x for x in self.targets if self.targets[x]['parent'] == misc.text_type(device) and self.targets[x]['status'] != misc.NEED_FORMAT ] if not children: self.target_added_cb(device) else: logging.debug('not adding device: 0 byte disk.')
def format_dest_clicked(self): item = self.__mainWindow.ui_dest_list.currentItem() if not item: return udi = misc.text_type(item.data(0,Qt.UserRole)) text = _('Are you sure you want to erase the entire disk?') res = KMessageBox.warningYesNo(self.__mainWindow,text) if res == KMessageBox.Yes: self.__mainWindow.setCursor(Qt.BusyCursor) # Disable simultaneous format attempts.. let current finish first self.__mainWindow.ui_format_dest.setEnabled(False) self.__backend.format(udi)
def add_target(self, target): logging.debug('add_target: %s' % str(target)) # use str() here, since it is originally a DBus.ObjectPath object _append_to_list_and_select(self.dest_treeview, [str(target)], force_selection=False) # XXX evand 2009-09-17: Find a label and icon for the device. v = self.backend.targets[target]['vendor'] m = self.backend.targets[target]['model'] d = self.backend.targets[target]['device'] l = self.backend.targets[target]['label'] name, icon = self.get_gnome_drive(d) if icon: self.icons[target] = icon if not l and name: self.names[target] = name self.pretty_names[target] = "%s %s (%s)" % (v, m, misc.text_type(d))
def set_font(self, family='Tahoma', size=13, bold=False): weight = bold and FW_BOLD or FW_NORMAL font = windll.gdi32.CreateFontW( size, # height of font 0, # average character width 0, # angle of escapement 0, # base-line orientation angle weight, # font weight 0, # FALSE italic attribute option 0, # FALSE underline attribute option 0, # FALSE strikeout attribute option 0, # DEFAULT_CHARSET character set identifier 0, # OUT_DEFAULT_PRECIS output precision 0, # CLIP_DEFAULT_PRECIS clipping precision 0, # NONANTIALIASED_QUALITY output quality 0, #0x20, DEFAULT_PITCH | FF_DONTCARE # pitch and family text_type(family) #TEXT("Verdana") # typeface name ) self._gdi_disposables.append(font) self._send_message(WM_SETFONT, font, True)
def update_target(self, udi): for i in range(0,self.__mainWindow.ui_dest_list.topLevelItemCount()): item = self.__mainWindow.ui_dest_list.topLevelItem(i) if misc.text_type(item.data(0,Qt.UserRole)) == udi: self.__mainWindow.ui_dest_list.emit( SIGNAL('itemChanged(item,0)')) break # Update persistence maximum value. self.__mainWindow.ui_persist_frame.setEnabled(False) self.__mainWindow.ui_start_button.setEnabled(False) target = self.__backend.targets[udi] persist_mb = target['persist'] / 1024 / 1024 if persist_mb > misc.MIN_PERSISTENCE: self.__mainWindow.ui_persist_frame.setEnabled(True) self.__mainWindow.ui_persist_slider.setRange( misc.MIN_PERSISTENCE, persist_mb) # Update install button state. status = target['status'] source = self.__backend.get_current_source() if not source: return stype = self.__backend.sources[source]['type'] if (status == misc.CAN_USE or (self.__mainWindow.ui_start_button.isEnabled() and stype == misc.SOURCE_IMG)): self.__mainWindow.ui_start_button.setEnabled(True) else: self.__mainWindow.ui_start_button.setEnabled(False) status = misc.NEED_FORMAT # Update the destination status message. if status == misc.CANNOT_USE: msg = _('The device is not large enough to hold this image.') elif status == misc.NEED_SPACE: msg = _('There is not enough free space for this image.') # FIXME RBG: 04/01/10 - Need Open button for folder to device for removing files elif status == misc.NEED_FORMAT: msg = _('The device needs to be formatted for use.') else: msg = '' self.__mainWindow.ui_dest_status.setText(msg)
def add_source(self, source): logging.debug('add_source: %s' % misc.text_type(source)) new_item = QTreeWidgetItem(self.__mainWindow.ui_source_list) new_item.setData(0,Qt.UserRole,source) # FIXME: # the new_item lines should be auto triggered onChange to the TreeWidget # when new_item is appended. new_item.setText(0,source) new_item.setIcon(0,KIcon("media-optical")) item = self.__mainWindow.ui_source_list.currentItem() if not item: item = self.__mainWindow.ui_source_list.topLevelItem(0) if item: self.__mainWindow.ui_source_list.setCurrentItem(item,True) # how does this all get added? here or elsewhere... # populate from device data if self.__backend is not None: new_item.setText(0,self.__backend.sources[source]['device']) # Strip as some derivates like to have whitespaces/newlines (e.g. netrunner) new_item.setText(1,self.__backend.sources[source]['label'].strip()) new_item.setText(2,misc.format_size(self.__backend.sources[source]['size']))
def get_device_num_for_letter(source): class STORAGE_DEVICE_NUMBER(ctypes.Structure): _fields_ = [("DeviceType", ctypes.wintypes.DWORD), ("DeviceNumber", ctypes.wintypes.ULONG), ("PartitionNumber", ctypes.wintypes.ULONG)] GENERIC_READ = (-2147483648) FILE_SHARE_READ = 1 OPEN_EXISTING = 3 INVALID_HANDLE_VALUE = 4294967295L NULL = 0 IOCTL_STORAGE_GET_DEVICE_NUMBER = 0x2D1080 CreateFileW = ctypes.windll.kernel32.CreateFileW DeviceIoControl = ctypes.windll.kernel32.DeviceIoControl if not os.path.isfile(source): # Strip the last character from the source (\). source = os.path.join('\\\\.\\', source[:2]) source_h = CreateFileW(misc.text_type(source), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL) if source_h == INVALID_HANDLE_VALUE: logging.error('Invalid handle.') return -1 num = STORAGE_DEVICE_NUMBER() c = ctypes.wintypes.DWORD() if not DeviceIoControl(source_h, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, ctypes.byref(num), ctypes.sizeof(num), ctypes.byref(c), NULL): logging.error('IOCTL_STORAGE_GET_DEVICE_NUMBER failed.') return -1 logging.debug( 'Device properties for %s: type %d, device %d, partition %d' % (source, num.DeviceType, num.DeviceNumber, num.PartitionNumber)) ctypes.windll.kernel32.CloseHandle(source_h) assert num.DeviceNumber != 0, 'Was going to return the first disk.' return num.DeviceNumber
def set_image(self, path, width=0, height=0): path = text_type(path) himage = windll.user32.LoadImageW(NULL, path, IMAGE_ICON, width, height, LR_LOADFROMFILE) self._gdi_disposables.append(himage) self._send_message(STM_SETIMAGE, IMAGE_ICON, himage)
def add_item(self, text): self._send_message(CB_ADDSTRING, 0, text_type(text))
def set_value(self, value): self._send_message(CB_SELECTSTRING, -1, text_type(value)) # CB_SETCURSEL, value, 0)
def set_icon(self, icon_path): if icon_path and os.path.isfile(icon_path): self.main_window._icon = windll.user32.LoadImageW( NULL, text_type(icon_path), IMAGE_ICON, 0, 0, LR_LOADFROMFILE) windll.user32.SendMessageW(self.main_window._hwnd, WM_SETICON, ICON_SMALL, self.main_window._icon)
def insert_column(self, col_name, col_width, idx=0): c = LVCOLUMN() c.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM c.pszText = text_type(col_name) c.cx = col_width self._send_message(LVM_INSERTCOLUMN, idx, byref(c))
def show_error_message(self, message, title=None): if not title: title = self.get_title() windll.user32.MessageBoxW(self.main_window._hwnd, text_type(message), text_type(title), MB_OK | MB_ICONERROR)
def show_info_message(self, message, title=None): if not title: title = self.get_title() windll.user32.MessageBoxW(self.main_window._hwnd, text_type(message), text_type(title), MB_OK | MB_ICONINFORMATION)