def OnButtonClicked(self, event): if event.GetId() == self.cancelButton.GetId(): self.Close() return name = self.nameInput.GetValue() url = self.urlInput.GetValue() isOnline = self.onlineRadio.GetValue() if not isOnline: url = None if not name or len(name) < 1: MessageBox("请输入Hosts名称", "提示", ICON_WARNING) elif isOnline and (not url or len(url) < 1): MessageBox("请输入在线Hosts地址", "提示", ICON_WARNING) else: if not self.iconComboBox.GetValue(): self.iconComboBox.SetValue("logo") if self.__hosts: self.__hosts["name"] = name self.__hosts["url"] = url self.__hosts["lastUpdateTime"] = Now() self.__hosts["icon"] = self.iconComboBox.GetValue() hostsId = self.__hosts['id'] else: hostsId = 0x1994 + len(Settings.settings["hosts"]) Settings.settings["hosts"].append( hostsDict(hostsId, name, url=url, lastUpdateTime=Now(), content="# Created by mHosts v%s, %s\n" % (Settings.version(), Now()), icon=self.iconComboBox.GetValue())) Settings.Save() self.__window.InitHostsTree(select=hostsId) self.Close()
def ApplyHosts(self, hostsId: int): commonHostsLines = "# hosts file apply by mHosts v%s, %s\n " % ( Settings.version(), Now()) currentHostsLines = "" currentHosts = None for hosts in Settings.settings["hosts"]: if hosts["id"] == hostsId: currentHostsLines += "# ---------------- %(name)s ----------------%(br)s%(content)s%(br)s" % { "name": hosts['name'], "br": os.linesep, "content": hosts['content'] } currentHosts = hosts if hosts["alwaysApply"]: commonHostsLines += "# ---------------- %(name)s ----------------%(br)s%(content)s%(br)s" % { "name": hosts['name'], "br": os.linesep, "content": hosts['content'] } hostsToApply = commonHostsLines + os.linesep + currentHostsLines try: if Hosts.Save2System(ParseHosts(hostsToApply)): for hosts in Settings.settings["hosts"]: hosts["active"] = hosts["id"] == hostsId refreshDnsRes = Hosts.TryFlushDNSCache() MessagePanel.Send("Hosts已设置为" + currentHosts["name"] + "\nDNS缓存刷新" + ("成功" if refreshDnsRes else "失败"), "保存成功", dpi=(self.dpiX, self.dpiY)) else: MessageBox("保存失败", "提示", ICON_ERROR) self.InitHostsTree(ID_SYSTEM_HOSTS) Settings.Save() except Exception as e: message = str(e) if "Permission denied" in message: MessageBox("尝试写入hosts时权限不足, 保存失败, 请以管理员身份运行该软件", "权限不足", ICON_ERROR) else: MessageBox(message, "保存时出错", ICON_ERROR)
def MainLoop(self): self.app.MainLoop() Settings.Save()