Skip to content

Commit

Permalink
Merge pull request #716 from PBH-BTN/master
Browse files Browse the repository at this point in the history
7.1.3
  • Loading branch information
Ghost-chu authored Nov 10, 2024
2 parents 1d6479b + c453b88 commit ddb76f0
Show file tree
Hide file tree
Showing 35 changed files with 572 additions and 593 deletions.
8 changes: 6 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.ghostchu.peerbanhelper</groupId>
<artifactId>peerbanhelper</artifactId>
<version>7.1.2</version>
<version>7.1.3</version>
<packaging>jar</packaging>

<name>PeerBanHelper</name>
Expand Down Expand Up @@ -485,7 +485,11 @@
<artifactId>xz</artifactId>
<version>1.10</version>
</dependency>

<dependency>
<groupId>com.formdev</groupId>
<artifactId>flatlaf-extras</artifactId>
<version>3.0</version>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public List<Torrent> getTorrents() {
fillTorrentPrivateField(qbTorrent);
}
return qbTorrent.stream().map(t -> (Torrent) t)
.filter(t-> !config.isIgnorePrivate() || !t.isPrivate())
.filter(t -> !config.isIgnorePrivate() || !t.isPrivate())
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -247,6 +247,9 @@ public List<Peer> getPeers(Torrent torrent) {
for (String s : peers.keySet()) {
JsonObject singlePeerObject = peers.getAsJsonObject(s);
QBittorrentPeer qbPeer = JsonUtil.getGson().fromJson(singlePeerObject.toString(), QBittorrentPeer.class);
if ("HTTP".equalsIgnoreCase(qbPeer.getConnection()) || "HTTPS".equalsIgnoreCase(qbPeer.getConnection()) || "Web".equalsIgnoreCase(qbPeer.getConnection())) {
continue;
}
if (qbPeer.getPeerAddress().getIp() == null || qbPeer.getPeerAddress().getIp().isBlank()) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
public final class QBittorrentPeer implements Peer {
@SerializedName("client")
private String client;
// @SerializedName("connection")
// private String connection;
@SerializedName("connection")
private String connection;
// @SerializedName("country")
// private String country;
// @SerializedName("country_code")
Expand Down Expand Up @@ -114,4 +114,7 @@ public String getRawIp() {
return rawIp == null ? ip : rawIp;
}

public String getConnection() {
return connection;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.ghostchu.peerbanhelper.gui.impl.swing;

import com.formdev.flatlaf.extras.FlatSVGIcon;
import com.formdev.flatlaf.util.SystemInfo;
import com.ghostchu.peerbanhelper.Main;
import com.ghostchu.peerbanhelper.downloader.DownloaderLastStatus;
import com.ghostchu.peerbanhelper.text.Lang;
import com.ghostchu.peerbanhelper.util.logger.LogEntry;
import lombok.Getter;
Expand All @@ -19,6 +21,8 @@
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;

Expand All @@ -36,7 +40,7 @@ public class MainWindow extends JFrame {
private JScrollPane loggerScrollPane;
@Nullable
@Getter
private TrayIcon trayIcon;
private SwingTray swingTrayDialog;
private boolean persistFlagTrayMessageSent;

public MainWindow(SwingGuiImpl swingGUI) {
Expand Down Expand Up @@ -87,7 +91,7 @@ public static void copyText(String content) {
}

private void minimizeToTray() {
if (trayIcon != null) {
if (swingTrayDialog != null) {
setVisible(false);
if (!persistFlagTrayMessageSent) {
persistFlagTrayMessageSent = true;
Expand All @@ -104,25 +108,64 @@ private void setupSystemTray() {
if (SystemTray.isSupported()) {
TrayIcon icon = new TrayIcon(Toolkit.getDefaultToolkit().getImage(Main.class.getResource("/assets/icon.png")));
icon.setImageAutoSize(true);
//创建弹出菜单
PopupMenu menu = new PopupMenu();
//添加一个用于退出的按钮
MenuItem item = new MenuItem("Exit");
item.addActionListener(e -> System.exit(0));
menu.add(item);
//添加弹出菜单到托盘图标
icon.setPopupMenu(menu);
SystemTray tray = SystemTray.getSystemTray();//获取系统托盘
icon.addActionListener(e -> setVisible(true));
SystemTray sysTray = SystemTray.getSystemTray();//获取系统托盘
try {
tray.add(icon);//将托盘图表添加到系统托盘
this.trayIcon = icon;
var tray = new SwingTray(icon, mouseEvent -> setVisible(true), mouseEvent -> updateTrayMenus());
sysTray.add(icon);//将托盘图表添加到系统托盘
updateTrayMenus();
this.swingTrayDialog = tray;
} catch (AWTException e) {
throw new RuntimeException(e);
}
}
}

private void updateTrayMenus() {
if (this.swingTrayDialog == null) return;
List<JMenuItem> items = new ArrayList<>();
JMenuItem openMainWindow = new JMenuItem(tlUI(Lang.GUI_MENU_SHOW_WINDOW), new FlatSVGIcon(Main.class.getResource("/assets/icon/tray/open.svg")));
JMenuItem openWebUI = new JMenuItem(tlUI(Lang.GUI_MENU_WEBUI_OPEN), new FlatSVGIcon(Main.class.getResource("/assets/icon/tray/browser.svg")));
JMenuItem quit = new JMenuItem(tlUI(Lang.GUI_MENU_QUIT), new FlatSVGIcon(Main.class.getResource("/assets/icon/tray/close.svg")));
openMainWindow.addActionListener(e -> setVisible(true));
openWebUI.addActionListener(e -> openWebUI());
quit.addActionListener(e -> System.exit(0));
items.add(menuDisplayItem(new JMenuItem(tlUI(Lang.GUI_MENU_STATS))));
items.add(menuBanStats());
items.add(menuDownloaderStats());
items.add(menuDisplayItem(new JMenuItem(tlUI(Lang.GUI_MENU_QUICK_OPERATIONS))));
items.add(openMainWindow);
items.add(openWebUI);
items.add(null);
items.add(quit);
this.swingTrayDialog.set(items);
}

private JMenuItem menuDownloaderStats() {
var totalDownloaders = 0L;
var healthDownloaders = 0L;
if (Main.getServer() != null) {
totalDownloaders = Main.getServer().getDownloaders().size();
healthDownloaders = Main.getServer().getDownloaders().stream().filter(m -> m.getLastStatus() == DownloaderLastStatus.HEALTHY).count();
}
return new JMenuItem(tlUI(Lang.GUI_MENU_STATS_DOWNLOADER, healthDownloaders, totalDownloaders), new FlatSVGIcon(Main.class.getResource("/assets/icon/tray/connection.svg")));
}

private JMenuItem menuBanStats() {
var bannedPeers = 0L;
var bannedIps = 0L;
var server = Main.getServer();
if (server != null) {
bannedIps = Main.getServer().getBannedPeers().values().stream().map(m -> m.getPeer().getAddress().getIp()).distinct().count();
bannedPeers = Main.getServer().getBannedPeers().values().size();
}
return new JMenuItem(tlUI(Lang.GUI_MENU_STATS_BANNED, bannedPeers, bannedIps), new FlatSVGIcon(Main.class.getResource("/assets/icon/tray/banned.svg")));
}

private JMenuItem menuDisplayItem(JMenuItem jMenuItem) {
jMenuItem.setEnabled(false);
return jMenuItem;
}

private JMenuBar setupMenuBar() {
JMenuBar menuBar = new JMenuBar();
menuBar.add(generateProgramMenu());
Expand Down Expand Up @@ -156,9 +199,7 @@ private JMenu generateWebUIMenu() {
JMenu webUIMenu = new JMenu(tlUI(Lang.GUI_MENU_WEBUI));
JMenuItem openWebUIMenuItem = new JMenuItem(tlUI(Lang.GUI_MENU_WEBUI_OPEN));
openWebUIMenuItem.addActionListener(e -> {
if (Main.getServer() != null && Main.getServer().getWebContainer() != null) {
swingGUI.openWebpage(URI.create("http://localhost:" + Main.getServer().getWebContainer().javalin().port() + "?token=" + Main.getServer().getWebContainer().getToken()));
}
this.openWebUI();
});
webUIMenu.add(openWebUIMenuItem);
JMenuItem copyWebUIToken = new JMenuItem(tlUI(Lang.GUI_COPY_WEBUI_TOKEN));
Expand All @@ -173,6 +214,12 @@ private JMenu generateWebUIMenu() {
return webUIMenu;
}

private void openWebUI() {
if (Main.getServer() != null && Main.getServer().getWebContainer() != null) {
swingGUI.openWebpage(URI.create("http://localhost:" + Main.getServer().getWebContainer().javalin().port() + "?token=" + Main.getServer().getWebContainer().getToken()));
}
}

public void sync() {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.formdev.flatlaf.FlatDarculaLaf;
import com.formdev.flatlaf.FlatIntelliJLaf;
import com.formdev.flatlaf.util.SystemInfo;
import com.ghostchu.peerbanhelper.Main;
import com.ghostchu.peerbanhelper.PeerBanHelperServer;
import com.ghostchu.peerbanhelper.exchange.ExchangeMap;
Expand Down Expand Up @@ -196,32 +195,39 @@ public void createDialog(Level level, String title, String description) {

@Override
public void createNotification(Level level, String title, String description) {
TrayIcon icon = mainWindow.getTrayIcon();
if (icon != null) {
if (level.equals(Level.WARNING)) {
icon.displayMessage(title, description, TrayIcon.MessageType.WARNING);
} else if (level.equals(Level.SEVERE)) {
icon.displayMessage(title, description, TrayIcon.MessageType.ERROR);
} else {
icon.displayMessage(title, description, TrayIcon.MessageType.INFO);
}
if (System.getProperty("os.name").contains("Windows")) {
CommonUtil.getScheduler().schedule(this::refreshTrayIcon, 5, TimeUnit.SECONDS);

var swingTray = mainWindow.getSwingTrayDialog();
if (swingTray != null) {
var icon = swingTray.getTrayIcon();
if (swingTray.getTrayIcon() != null) {
if (level.equals(Level.WARNING)) {
icon.displayMessage(title, description, TrayIcon.MessageType.WARNING);
} else if (level.equals(Level.SEVERE)) {
icon.displayMessage(title, description, TrayIcon.MessageType.ERROR);
} else {
icon.displayMessage(title, description, TrayIcon.MessageType.INFO);
}
if (System.getProperty("os.name").contains("Windows")) {
CommonUtil.getScheduler().schedule(this::refreshTrayIcon, 5, TimeUnit.SECONDS);
}
return;
}
} else {
super.createNotification(level, title, description);
}
super.createNotification(level, title, description);
}

private synchronized void refreshTrayIcon() {
TrayIcon icon = mainWindow.getTrayIcon();
if (icon != null) {
try {
SystemTray tray = SystemTray.getSystemTray();
tray.remove(icon); // fix https://github.com/PBH-BTN/PeerBanHelper/issues/515
tray.add(icon);
} catch (AWTException e) {
throw new RuntimeException(e);
var swingTray = mainWindow.getSwingTrayDialog();
if (swingTray != null) {
var icon = swingTray.getTrayIcon();
if (icon != null) {
try {
SystemTray tray = SystemTray.getSystemTray();
tray.remove(icon); // fix https://github.com/PBH-BTN/PeerBanHelper/issues/515
tray.add(icon);
} catch (AWTException e) {
throw new RuntimeException(e);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.ghostchu.peerbanhelper.gui.impl.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.function.Consumer;

public class SwingTray {

private final JDialog jDialog;
private final JPopupMenu jPopupMenu;
private final TrayIcon trayIcon;

public SwingTray(TrayIcon trayIcon, Consumer<MouseEvent> clickCallback, Consumer<MouseEvent> rightClickCallback) {
this.trayIcon = trayIcon;
this.jDialog = new JDialog();
jDialog.setUndecorated(true);
jDialog.setSize(1, 1);
this.jPopupMenu = new JPopupMenu() {
@Override
public void firePopupMenuWillBecomeInvisible() {
jDialog.setVisible(false);
}
};
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
// 左键单击
if (e.getButton() == 1) {
clickCallback.accept(e);
return;
}
if (e.getButton() == 3 && e.isPopupTrigger()) {
rightClickCallback.accept(e);
// 获取屏幕相对位置
Point point = MouseInfo.getPointerInfo().getLocation();
// 将jDialog设置为鼠标位置
jDialog.setLocation(point.x, point.y);
// 显示载体
jDialog.setVisible(true);
var dimension = jPopupMenu.getPreferredSize();
jPopupMenu.setSize((int) dimension.getWidth() + 1, (int) dimension.getHeight() + 1);
// 在载体的0,0处显示对话框
jPopupMenu.show(jDialog, 0, 0);
}
}
});
}

public void set(List<JMenuItem> items) {
jPopupMenu.removeAll();
items.forEach(ele->{
if(ele == null) {
jPopupMenu.addSeparator();
}else{
jPopupMenu.add(ele);
}
});
}

public TrayIcon getTrayIcon() {
return trayIcon;
}

public JDialog getjDialog() {
return jDialog;
}

public JPopupMenu getjPopupMenu() {
return jPopupMenu;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/ghostchu/peerbanhelper/text/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public enum Lang {
BTN_RULES_SCRIPT_COMPILING,
BTN_RULES_SCRIPT_COMPILED,
BTN_SERVICES_NEED_RESTART,
EXPRESS_RULE_ENGINE_SAVED;
EXPRESS_RULE_ENGINE_SAVED, GUI_MENU_SHOW_WINDOW, GUI_MENU_STATS_BANNED, GUI_MENU_STATS_DOWNLOADER, GUI_MENU_QUICK_OPERATIONS, GUI_MENU_STATS;

public String getKey() {
return name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static String[] convert(String locale, @Nullable Object... args) {
* Loading Crowdin OTA module and i18n system
*/
public void load() {
log.info("Loading up translations, this may need a while...");
log.info("Loading up translations, this may take a while...");
//TODO: This will break the message processing system in-game until loading finished, need to fix it.
this.reset();
// first, we need load built-in fallback translation.
Expand Down
Loading

0 comments on commit ddb76f0

Please sign in to comment.