81 lines
3.1 KiB
Java
81 lines
3.1 KiB
Java
package ctbrec.ui.action;
|
|
|
|
import static ctbrec.io.HttpConstants.*;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import ctbrec.Config;
|
|
import ctbrec.Model;
|
|
import ctbrec.recorder.Recorder;
|
|
import ctbrec.ui.controls.Dialogs;
|
|
import javafx.application.Platform;
|
|
import javafx.scene.control.Button;
|
|
import okhttp3.Request;
|
|
import okhttp3.Response;
|
|
|
|
public class CheckModelAccountAction {
|
|
private static final Logger LOG = LoggerFactory.getLogger(CheckModelAccountAction.class);
|
|
|
|
private Button b;
|
|
|
|
private Recorder recorder;
|
|
|
|
public CheckModelAccountAction(Button b, Recorder recorder) {
|
|
this.b = b;
|
|
this.recorder = recorder;
|
|
|
|
}
|
|
|
|
public void execute() {
|
|
String buttonText = b.getText();
|
|
b.setDisable(true);
|
|
Runnable checker = (() -> {
|
|
List<Model> deletedAccounts = new ArrayList<>();
|
|
try {
|
|
List<Model> models = recorder.getModels();
|
|
int total = models.size();
|
|
for (int i = 0; i < total; i++) {
|
|
final int counter = i+1;
|
|
Platform.runLater(() -> b.setText(buttonText + ' ' + counter + '/' + total));
|
|
Model modelToCheck = models.get(i);
|
|
Request req = new Request.Builder()
|
|
.url(modelToCheck.getUrl())
|
|
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
|
|
.build();
|
|
try(Response response = modelToCheck.getSite().getHttpClient().execute(req)) {
|
|
if(!response.isSuccessful() && response.code() == 404) {
|
|
deletedAccounts.add(modelToCheck);
|
|
}
|
|
} catch (IOException e) {
|
|
LOG.warn("Couldn't check, if model account still exists", e);
|
|
}
|
|
}
|
|
} finally {
|
|
Platform.runLater(() -> {
|
|
b.setDisable(false);
|
|
b.setText(buttonText);
|
|
if (!deletedAccounts.isEmpty()) {
|
|
StringBuilder sb = new StringBuilder();
|
|
for (Model deletedModel : deletedAccounts) {
|
|
String name = deletedModel.getDisplayName() + " ".repeat(30);
|
|
name = name.substring(0, 30);
|
|
sb.append(name).append(' ').append('(').append(deletedModel.getUrl()).append(')').append('\n');
|
|
}
|
|
boolean remove = Dialogs.showConfirmDialog("Deleted Accounts", sb.toString(),
|
|
"The following accounts seem to have been deleted. Do you want to remove them?", b.getScene());
|
|
if (remove) {
|
|
new StopRecordingAction(b, deletedAccounts, recorder).execute();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
});
|
|
new Thread(checker).start();
|
|
}
|
|
}
|