Skip to content

Commit

Permalink
Add an option to disable the add torrent options dialog
Browse files Browse the repository at this point in the history
When unchecked, any torrent will be added automatically to the default
download directory
  • Loading branch information
urandom committed Jan 3, 2015
1 parent d1766c1 commit 45ffdfd
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 49 deletions.
1 change: 1 addition & 0 deletions gearshift/src/main/java/org/sugr/gearshift/G.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public final class G {
public static final String PREF_FULL_UPDATE = "full_update";
public static final String PREF_UPDATE_INTERVAL = "update_interval";
public static final String PREF_SHOW_STATUS = "show_status";
public static final String PREF_SHOW_ADD_DIALOG = "show_add_dialog";
public static final String PREF_AUTO_UPDATE_CHECK = "auto_update_check";

public static final String PREF_PROFILES = "profiles";
Expand Down
115 changes: 71 additions & 44 deletions gearshift/src/main/java/org/sugr/gearshift/ui/TorrentListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,23 @@ private void consumeIntent() {
private void showNewTorrentDialog(final Uri data, final String fileURI,
final String filePath, final Uri documentUri) {

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (!prefs.getBoolean(G.PREF_SHOW_ADD_DIALOG, true)) {
boolean startPaused = prefs.getBoolean(G.PREF_START_PAUSED, false);

if (data != null && data.getScheme().equals("magnet")) {
addMagnetLink(profile, data, null, startPaused);
} else if (fileURI != null) {
addTorrentFile(profile, fileURI, filePath, documentUri,
null, startPaused, prefs.getBoolean(G.PREF_DELETE_LOCAL, false));
} else {
return;
}

intentConsumed = true;
return;
}

newTorrentDialogVisible = true;

int title;
Expand All @@ -725,10 +742,8 @@ public void onClick(DialogInterface dialog, int id) {

LocationDialogHelper.Location location = locationDialogHelper.getLocation();

manager.addTorrent(location.profile.getId(), Uri.decode(data.toString()), null,
location.directory, location.isPaused, null, null);
addMagnetLink(location.profile, data, location.directory, location.isPaused);

setRefreshing(true, DataService.Requests.ADD_TORRENT);
intentConsumed = true;
newTorrentDialogVisible = false;
}
Expand All @@ -745,48 +760,9 @@ public void onClick(final DialogInterface dialog, int which) {

LocationDialogHelper.Location location = locationDialogHelper.getLocation();

BufferedReader reader = null;

try {
File file = new File(new URI(fileURI));

reader = new BufferedReader(new FileReader(file));
StringBuilder filedata = new StringBuilder();
String line;

while ((line = reader.readLine()) != null) {
filedata.append(line).append("\n");
}

if (!file.delete()) {
Toast.makeText(TorrentListActivity.this,
R.string.error_deleting_torrent_file, Toast.LENGTH_SHORT).show();
}

String path = filePath;
Uri uri = documentUri;
if (!location.deleteLocal) {
path = null;
uri = null;
}

manager.addTorrent(location.profile.getId(), null, filedata.toString(),
location.directory, location.isPaused,
path, uri);
addTorrentFile(location.profile, fileURI, filePath, documentUri,
location.directory, location.isPaused, location.deleteLocal);

setRefreshing(true, DataService.Requests.ADD_TORRENT);
} catch (Exception e) {
Toast.makeText(TorrentListActivity.this,
R.string.error_reading_torrent_file, Toast.LENGTH_SHORT).show();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
intentConsumed = true;
newTorrentDialogVisible = false;
}
Expand Down Expand Up @@ -862,6 +838,57 @@ public void onClick(DialogInterface dialog, int which) {

}

private void addMagnetLink(TransmissionProfile profile, Uri magnet, String directory, boolean isPaused) {
manager.addTorrent(profile.getId(), Uri.decode(magnet.toString()), null, directory, isPaused, null, null);

setRefreshing(true, DataService.Requests.ADD_TORRENT);
}

private void addTorrentFile(TransmissionProfile profile, String fileURI, String filePath,
Uri documentUri, String directory, boolean isPaused, boolean deleteLocal) {
BufferedReader reader = null;

try {
File file = new File(new URI(fileURI));

reader = new BufferedReader(new FileReader(file));
StringBuilder filedata = new StringBuilder();
String line;

while ((line = reader.readLine()) != null) {
filedata.append(line).append("\n");
}

if (!file.delete()) {
Toast.makeText(TorrentListActivity.this,
R.string.error_deleting_torrent_file, Toast.LENGTH_SHORT).show();
}

String path = filePath;
Uri uri = documentUri;
if (!deleteLocal) {
path = null;
uri = null;
}

manager.addTorrent(profile.getId(), null, filedata.toString(), directory, isPaused,
path, uri);

setRefreshing(true, DataService.Requests.ADD_TORRENT);
} catch (Exception e) {
Toast.makeText(TorrentListActivity.this,
R.string.error_reading_torrent_file, Toast.LENGTH_SHORT).show();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

private class ReadTorrentDataTask extends AsyncTask<Uri, Void, ReadTorrentDataTask.TaskData> {
public class TaskData {
public File file;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
return null;
}
return new TorrentTrafficLoader(getActivity(), context.getProfile().getId(),
sharedPrefs.getBoolean(G.PREF_SHOW_STATUS, false), false, false);
sharedPrefs.getBoolean(G.PREF_SHOW_STATUS, true), false, false);
}
return null;
}
Expand All @@ -372,7 +372,7 @@ public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
Toolbar status = (Toolbar) getView().findViewById(R.id.status_bar);
TextView statusBarText = (TextView) status.findViewById(R.id.status_bar_text);

if (!sharedPrefs.getBoolean(G.PREF_SHOW_STATUS, false)) {
if (!sharedPrefs.getBoolean(G.PREF_SHOW_STATUS, true)) {
return;
}

Expand Down Expand Up @@ -998,7 +998,7 @@ private void toggleStatusBar() {
}

private void toggleStatusBar(View status) {
if (sharedPrefs.getBoolean(G.PREF_SHOW_STATUS, false)) {
if (sharedPrefs.getBoolean(G.PREF_SHOW_STATUS, true)) {
status.setVisibility(View.VISIBLE);
status.setTranslationY(100);
status.animate().alpha(1f).translationY(0).setStartDelay(500);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private enum Type {
return null;
}

boolean showStatus = sharedPrefs.getBoolean(G.PREF_SHOW_STATUS, false);
boolean showStatus = sharedPrefs.getBoolean(G.PREF_SHOW_STATUS, true);
boolean directoriesEnabled = sharedPrefs.getBoolean(G.PREF_FILTER_DIRECTORIES, true);
boolean trackersEnabled = sharedPrefs.getBoolean(G.PREF_FILTER_TRACKERS, false);

Expand Down
1 change: 1 addition & 0 deletions gearshift/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@
<string name="status_bar_limit_format"><![CDATA[ (<i>%1$s</i>)]]></string>
<string name="user_interface_header">User interface</string>
<string name="show_status_bar">Status bar below the torrent list</string>
<string name="show_add_dialog">Show options dialog when adding torrents</string>
<string name="error_deleting_torrent_file">Unable to delete torrent file</string>
<string name="hint_download_directory">Download directory</string>
<string name="spinner_default_directory">Default directory</string>
Expand Down
7 changes: 6 additions & 1 deletion gearshift/src/main/res/xml/general_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@
<CheckBoxPreference
android:key="show_status"
android:title="@string/show_status_bar"
android:defaultValue="false" />
android:defaultValue="true" />

<CheckBoxPreference
android:key="show_add_dialog"
android:title="@string/show_add_dialog"
android:defaultValue="true" />

</PreferenceCategory>

Expand Down

0 comments on commit 45ffdfd

Please sign in to comment.