+ GUI support for setting up osdb and sublight logins

This commit is contained in:
Reinhard Pointner 2013-02-26 06:21:02 +00:00
parent a56c8bbc66
commit ae111086f8
9 changed files with 173 additions and 42 deletions

View File

@ -210,7 +210,6 @@ public class Main {
// hook donation reminder into rename history
if (useDonationReminder()) {
System.out.println("Main.main()");
final HistoryStorage fileStorage = HistorySpooler.getInstance().getPersistentHistory();
HistorySpooler.getInstance().setPersistentHistory(new HistoryStorage() {

View File

@ -144,7 +144,11 @@ public final class Settings {
public void put(String key, String value) {
prefs.put(key, value);
if (value != null) {
prefs.put(key, value);
} else {
remove(key);
}
}

View File

@ -21,3 +21,5 @@ serienjunkies.apikey: 9fbhw9uebfiwvbefzuwv
fanart.tv.apikey: 780b986b22c35e6f7a134a2f392c2deb
acoustid.apikey: 0B3qZnQc
pushover.apikey: wcckDz3oygHSU2SdIptvnHxJ92SQKK
sublight.clientid: FileBot2
sublight.apikey: 79f7a868-c28c-446f-a58e-3637ca24c87a

View File

@ -224,16 +224,31 @@ public final class WebServices {
}
private static String[] getLogin(String key) {
// try system property first
String login = System.getProperty(key);
// try settings second
if (login == null) {
login = Settings.forPackage(WebServices.class).get(key, ":");
public static String[] getLogin(String key) {
return Settings.forPackage(WebServices.class).get(key, ":").split(":", 2);
}
public static void setLogin(String id, String user, String password) {
Settings settings = Settings.forPackage(WebServices.class);
String value = user.length() > 0 && password.length() > 0 ? user + ":" + password : null;
if (value == null) {
user = "";
password = "";
}
return login.split(":", 2);
if (id.equals("osdb.user")) {
settings.put(id, value);
OpenSubtitles.setUser(user, password);
} else if (id.equals("sublight.user")) {
settings.put(id, value);
Sublight.setUser(user, password);
} else if (id.equals("sublight.client")) {
settings.put(id, value);
Sublight.setClient(user, password);
} else {
throw new IllegalArgumentException();
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 915 B

View File

@ -68,15 +68,13 @@ public abstract class AbstractSearchPanel<S, E> extends JComponent {
tabbedPaneGroup.setBorder(BorderFactory.createTitledBorder("Search Results"));
tabbedPaneGroup.add(tabbedPane, "grow, wrap");
setLayout(new MigLayout("nogrid, fill, insets 10px 10px 15px 10px", "align center", "[pref!]10px[fill]"));
add(searchTextField);
add(new JButton(searchAction), "gap 18px, wrap");
add(tabbedPaneGroup, "grow");
add(new JButton(searchAction), "gap 18px, id search");
add(tabbedPaneGroup, "newline, grow");
searchTextField.getEditor().setAction(searchAction);
searchTextField.getSelectButton().setModel(Arrays.asList(getSearchEngines()));
searchTextField.getSelectButton().setLabelProvider(getSearchEngineLabelProvider());
@ -98,7 +96,6 @@ public abstract class AbstractSearchPanel<S, E> extends JComponent {
});
AutoCompleteSupport.install(searchTextField.getEditor(), searchHistory).setFilterMode(TextMatcherEditor.CONTAINS);
installAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), searchAction);
}

View File

@ -2,27 +2,48 @@
package net.sourceforge.filebot.ui.subtitle;
import static net.sourceforge.filebot.Settings.*;
import static net.sourceforge.filebot.ui.LanguageComboBoxModel.*;
import static net.sourceforge.filebot.ui.NotificationLogging.*;
import static net.sourceforge.tuned.ui.TunedUtilities.*;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dialog.ModalityType;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.geom.Path2D;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import net.miginfocom.swing.MigLayout;
import net.sourceforge.filebot.ResourceManager;
import net.sourceforge.filebot.Settings;
import net.sourceforge.filebot.WebServices;
import net.sourceforge.filebot.ui.AbstractSearchPanel;
import net.sourceforge.filebot.ui.Language;
import net.sourceforge.filebot.ui.LanguageComboBox;
import net.sourceforge.filebot.ui.SelectDialog;
import net.sourceforge.filebot.web.OpenSubtitlesClient;
import net.sourceforge.filebot.web.SearchResult;
import net.sourceforge.filebot.web.SublightSubtitleClient;
import net.sourceforge.filebot.web.SubtitleDescriptor;
import net.sourceforge.filebot.web.SubtitleProvider;
import net.sourceforge.filebot.web.VideoHashSubtitleService;
@ -34,19 +55,19 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, Subtitl
private LanguageComboBox languageComboBox = new LanguageComboBox(this, ALL_LANGUAGES);
public SubtitlePanel() {
historyPanel.setColumnHeader(0, "Show / Movie");
historyPanel.setColumnHeader(1, "Number of Subtitles");
// add after text field
add(languageComboBox, "gap indent", 1);
add(createImageButton(setUserAction), "width 26px!, height 26px!, gap rel", 2);
// add at the top right corner
add(dropTarget, "width 1.6cm!, height 1.2cm!, pos n 0% 100% n", 0);
}
private final SubtitleDropTarget dropTarget = new SubtitleDropTarget() {
@Override
@ -54,20 +75,20 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, Subtitl
return WebServices.getVideoHashSubtitleServices();
}
@Override
public SubtitleProvider[] getSubtitleProviders() {
return WebServices.getSubtitleProviders();
}
@Override
public String getQueryLanguage() {
// use currently selected language for drop target
return languageComboBox.getModel().getSelectedItem() == ALL_LANGUAGES ? null : languageComboBox.getModel().getSelectedItem().getName();
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
@ -92,25 +113,25 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, Subtitl
}
};
@Override
protected SubtitleProvider[] getSearchEngines() {
return WebServices.getSubtitleProviders();
}
@Override
protected LabelProvider<SubtitleProvider> getSearchEngineLabelProvider() {
return SimpleLabelProvider.forClass(SubtitleProvider.class);
}
@Override
protected Settings getSettings() {
return Settings.forPackage(SubtitlePanel.class);
}
@Override
protected SubtitleRequestProcessor createRequestProcessor() {
SubtitleProvider provider = searchTextField.getSelectButton().getSelectedValue();
@ -120,13 +141,13 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, Subtitl
return new SubtitleRequestProcessor(new SubtitleRequest(provider, text, language));
}
protected static class SubtitleRequest extends Request {
private final SubtitleProvider provider;
private final Language language;
public SubtitleRequest(SubtitleProvider provider, String searchText, Language language) {
super(searchText);
@ -134,32 +155,32 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, Subtitl
this.language = language;
}
public SubtitleProvider getProvider() {
return provider;
}
public String getLanguageName() {
return language == ALL_LANGUAGES ? null : language.getName();
}
}
protected static class SubtitleRequestProcessor extends RequestProcessor<SubtitleRequest, SubtitlePackage> {
public SubtitleRequestProcessor(SubtitleRequest request) {
super(request, new SubtitleDownloadComponent());
}
@Override
public Collection<SearchResult> search() throws Exception {
return request.getProvider().search(request.getSearchText());
}
@Override
public Collection<SubtitlePackage> fetch() throws Exception {
List<SubtitlePackage> packages = new ArrayList<SubtitlePackage>();
@ -171,38 +192,38 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, Subtitl
return packages;
}
@Override
public URI getLink() {
return request.getProvider().getSubtitleListLink(getSearchResult(), request.getLanguageName());
}
@Override
public void process(Collection<SubtitlePackage> subtitles) {
getComponent().setLanguageVisible(request.getLanguageName() == null);
getComponent().getPackageModel().addAll(subtitles);
}
@Override
public SubtitleDownloadComponent getComponent() {
return (SubtitleDownloadComponent) super.getComponent();
}
@Override
public String getStatusMessage(Collection<SubtitlePackage> result) {
return (result.isEmpty()) ? "No subtitles found" : String.format("%d subtitles", result.size());
}
@Override
public Icon getIcon() {
return request.provider.getIcon();
}
@Override
protected void configureSelectDialog(SelectDialog<SearchResult> selectDialog) {
super.configureSelectDialog(selectDialog);
@ -211,4 +232,99 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, Subtitl
}
protected final Action setUserAction = new AbstractAction("Set User", ResourceManager.getIcon("action.user")) {
@Override
public void actionPerformed(ActionEvent evt) {
final JDialog authPanel = new JDialog(getWindow(SubtitlePanel.this), ModalityType.APPLICATION_MODAL);
authPanel.setTitle("Login");
authPanel.setLocation(getOffsetLocation(authPanel.getOwner()));
JPanel osdbGroup = new JPanel(new MigLayout("fill, insets panel"));
osdbGroup.setBorder(new TitledBorder("OpenSubtitles"));
osdbGroup.add(new JLabel("Username:"), "gap rel");
final JTextField osdbUser = new JTextField(12);
osdbGroup.add(osdbUser, "growx, wrap rel");
osdbGroup.add(new JLabel("Password:"), "gap rel");
final JPasswordField osdbPass = new JPasswordField(12);
osdbGroup.add(osdbPass, "growx, wrap rel");
JPanel sublGroup = new JPanel(new MigLayout("fill, insets panel"));
sublGroup.setBorder(new TitledBorder("Sublight"));
sublGroup.add(new JLabel("Username:"), "gap rel");
final JTextField sublUser = new JTextField(12);
sublGroup.add(sublUser, "growx, wrap rel");
sublGroup.add(new JLabel("Password:"), "gap rel");
final JPasswordField sublPass = new JPasswordField(12);
sublGroup.add(sublPass, "growx, wrap rel");
JRootPane container = authPanel.getRootPane();
container.setLayout(new MigLayout("fill, insets dialog"));
container.removeAll();
container.add(osdbGroup, "growx, wrap");
container.add(sublGroup, "growx, wrap");
Action ok = new AbstractAction("OK") {
@Override
public void actionPerformed(ActionEvent evt) {
authPanel.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
boolean approved = true;
try {
if (osdbUser.getText().length() > 0 && osdbPass.getPassword().length > 0) {
OpenSubtitlesClient osdb = new OpenSubtitlesClient(String.format("%s %s", getApplicationName(), getApplicationVersion()));
osdb.setUser(osdbUser.getText(), new String(osdbPass.getPassword()));
osdb.login();
}
} catch (Exception e) {
UILogger.log(Level.WARNING, "OpenSubtitles: " + e.getMessage());
approved = false;
}
try {
if (sublUser.getText().length() > 0 && sublPass.getPassword().length > 0) {
SublightSubtitleClient sublight = new SublightSubtitleClient();
sublight.setClient(getApplicationProperty("sublight.clientid"), getApplicationProperty("sublight.apikey"));
sublight.setUser(sublUser.getText(), new String(sublPass.getPassword()));
sublight.login();
}
} catch (Exception e) {
UILogger.log(Level.WARNING, "Sublight: " + e.getMessage());
approved = false;
}
authPanel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
if (approved) {
WebServices.setLogin("osdb.user", osdbUser.getText(), new String(osdbPass.getPassword()));
WebServices.setLogin("sublight.user", sublUser.getText(), new String(sublPass.getPassword()));
WebServices.setLogin("sublight.client", getApplicationProperty("sublight.clientid"), getApplicationProperty("sublight.apikey"));
authPanel.setVisible(false);
}
}
};
Action cancel = new AbstractAction("Cancel") {
@Override
public void actionPerformed(ActionEvent evt) {
authPanel.setVisible(false);
}
};
container.add(new JButton(cancel), "tag cancel, split 2");
container.add(new JButton(ok), "tag ok");
// restore values
String[] osdbAuth = WebServices.getLogin("osdb.user");
osdbUser.setText(osdbAuth[0]);
osdbPass.setText(osdbAuth[1]);
String[] sublAuth = WebServices.getLogin("sublight.user");
sublUser.setText(sublAuth[0]);
sublPass.setText(sublAuth[1]);
authPanel.pack();
authPanel.setVisible(true);
}
};
}

View File

@ -322,7 +322,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
}
protected synchronized void login() throws Exception {
public synchronized void login() throws Exception {
if (!xmlrpc.isLoggedOn()) {
xmlrpc.login(username, password, "en");
}

View File

@ -358,13 +358,12 @@ public class SublightSubtitleClient implements SubtitleProvider, VideoHashSubtit
public synchronized void setClient(String id, String key) {
clientInfo.setClientId(id);
clientInfo.setApiKey(key);
}
public synchronized void setUser(String username, String password) {
this.username = username;
this.passwordHash = getPasswordHash(password);
this.passwordHash = password != null && password.length() > 0 ? getPasswordHash(password) : null;
}
@ -379,7 +378,7 @@ public class SublightSubtitleClient implements SubtitleProvider, VideoHashSubtit
}
protected synchronized void login() throws WebServiceException {
public synchronized void login() throws WebServiceException {
if (clientInfo.getClientId() == null || clientInfo.getClientId().isEmpty()) {
throw new IllegalStateException("Sublight login has not been configured");
}
@ -439,7 +438,6 @@ public class SublightSubtitleClient implements SubtitleProvider, VideoHashSubtit
}
}
protected final Timer logoutTimer = new Timer() {
@Override