2014-04-07 02:26:30 +02:00
|
|
|
package com.rarchives.ripme.ui;
|
|
|
|
|
|
|
|
import java.awt.HeadlessException;
|
|
|
|
import java.awt.Toolkit;
|
|
|
|
import java.awt.datatransfer.DataFlavor;
|
|
|
|
import java.awt.datatransfer.UnsupportedFlavorException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2016-10-01 22:22:38 +02:00
|
|
|
import static com.rarchives.ripme.App.logger;
|
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
class ClipboardUtils {
|
2014-04-07 02:26:30 +02:00
|
|
|
private static AutoripThread autoripThread = new AutoripThread();
|
2017-05-10 00:22:55 +02:00
|
|
|
|
2014-04-07 02:26:30 +02:00
|
|
|
public static void setClipboardAutoRip(boolean enabled) {
|
|
|
|
if (enabled) {
|
|
|
|
autoripThread.kill();
|
|
|
|
autoripThread = new AutoripThread();
|
|
|
|
autoripThread.isRunning = true;
|
|
|
|
autoripThread.start();
|
|
|
|
} else {
|
|
|
|
autoripThread.kill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static boolean getClipboardAutoRip() {
|
|
|
|
return autoripThread.isRunning;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getClipboardString() {
|
|
|
|
try {
|
|
|
|
return (String) Toolkit
|
|
|
|
.getDefaultToolkit()
|
|
|
|
.getSystemClipboard()
|
|
|
|
.getData(DataFlavor.stringFlavor);
|
2016-10-01 22:22:38 +02:00
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
logger.error("Caught and recovered from IllegalStateException: " + e.getMessage());
|
2017-10-24 16:33:28 +02:00
|
|
|
} catch (HeadlessException | IOException | UnsupportedFlavorException e) {
|
2014-04-07 02:26:30 +02:00
|
|
|
e.printStackTrace();
|
2017-05-10 00:22:55 +02:00
|
|
|
}
|
2014-04-07 02:26:30 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class AutoripThread extends Thread {
|
2017-10-24 16:33:28 +02:00
|
|
|
volatile boolean isRunning = false;
|
|
|
|
private Set<String> rippedURLs = new HashSet<>();
|
2014-04-07 02:26:30 +02:00
|
|
|
|
|
|
|
public void run() {
|
|
|
|
isRunning = true;
|
|
|
|
try {
|
|
|
|
while (isRunning) {
|
|
|
|
// Check clipboard
|
|
|
|
String clipboard = ClipboardUtils.getClipboardString();
|
|
|
|
if (clipboard != null) {
|
|
|
|
Pattern p = Pattern.compile(
|
2018-02-10 17:05:17 +01:00
|
|
|
// TODO: This regex is a monster and doesn't match all links; It needs to be rewritten
|
2017-10-24 16:33:28 +02:00
|
|
|
"\\b(((ht|f)tp(s?)://|~/|/)|www.)" +
|
2017-05-10 00:22:55 +02:00
|
|
|
"(\\w+:\\w+@)?(([-\\w]+\\.)+(com|org|net|gov" +
|
|
|
|
"|mil|biz|info|mobi|name|aero|jobs|museum" +
|
2018-02-10 17:05:17 +01:00
|
|
|
"|travel|cafe|[a-z]{2}))(:[\\d]{1,5})?" +
|
2017-10-24 16:33:28 +02:00
|
|
|
"(((/([-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|/)+|\\?|#)?" +
|
2017-05-10 00:22:55 +02:00
|
|
|
"((\\?([-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
|
|
|
|
"([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)" +
|
|
|
|
"(&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
|
|
|
|
"([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*" +
|
2014-04-07 02:26:30 +02:00
|
|
|
"(#([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?\\b");
|
|
|
|
Matcher m = p.matcher(clipboard);
|
|
|
|
while (m.find()) {
|
|
|
|
String url = m.group();
|
|
|
|
if (!rippedURLs.contains(url)) {
|
|
|
|
rippedURLs.add(url);
|
2014-04-08 06:57:18 +02:00
|
|
|
// TODO Queue rip instead of just starting it
|
2014-04-07 02:26:30 +02:00
|
|
|
MainWindow.ripAlbumStatic(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Thread.sleep(1000);
|
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 00:22:55 +02:00
|
|
|
|
2014-04-07 02:26:30 +02:00
|
|
|
public void kill() {
|
|
|
|
isRunning = false;
|
|
|
|
}
|
|
|
|
}
|