Skip to content

Commit

Permalink
Support MOC history elsewhere; increase limit
Browse files Browse the repository at this point in the history
  • Loading branch information
parg committed Oct 10, 2023
1 parent 785f1f7 commit c9bac3c
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 60 deletions.
40 changes: 40 additions & 0 deletions uis/src/com/biglybt/ui/swt/MenuBuildUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1687,4 +1687,44 @@ public void handleEvent(Event event){

return( openWithItem );
}

public static void
addMOCHistory(
Menu moc_menu,
Consumer<String> moc_setter )
{
List<String> moc_hist = COConfigurationManager.getStringListParameter( "open.torrent.window.moc.history" );

if ( !moc_hist.isEmpty()){

new org.eclipse.swt.widgets.MenuItem( moc_menu, SWT.SEPARATOR );

for ( String hist: moc_hist ){

org.eclipse.swt.widgets.MenuItem hist_item = new org.eclipse.swt.widgets.MenuItem( moc_menu, SWT.PUSH );

hist_item.setText( hist );

hist_item.addListener( SWT.Selection, (ev)->moc_setter.accept( hist ));
}
}
}

public static void
addToMOCHistory(
String path )
{
List<String> moc_hist = COConfigurationManager.getStringListParameter( "open.torrent.window.moc.history" );

moc_hist.remove( path );

moc_hist.add( 0, path );

if ( moc_hist.size() > 10 ){

moc_hist.remove( moc_hist.size()-1 );
}

COConfigurationManager.setParameter( "open.torrent.window.moc.history", moc_hist );
}
}
34 changes: 33 additions & 1 deletion uis/src/com/biglybt/ui/swt/TorrentMenuFancy.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.function.Consumer;

import com.biglybt.ui.swt.mainwindow.*;
import org.eclipse.swt.SWT;
Expand Down Expand Up @@ -2620,6 +2621,23 @@ public void run(DownloadManager[] dms) {
buildMenu(
Menu moc_menu )
{
// existing

String existing_moc = TorrentUtil.getMOC( dms );

if ( existing_moc != null ){

MenuItem existing_item = new MenuItem( moc_menu, SWT.PUSH );

existing_item.setText( "[" + existing_moc + "]" );

existing_item.setEnabled( false );

new MenuItem( moc_menu, SWT.SEPARATOR );
}

// clear

MenuItem clear_item = new MenuItem( moc_menu, SWT.PUSH);

Messages.setLanguageText( clear_item, "Button.clear" );
Expand All @@ -2633,18 +2651,32 @@ public void run(DownloadManager[] dms) {

clear_item.setEnabled( f_canClearMOC );

// set

Consumer<String> moc_setter = (path)->{

MenuBuildUtils.addToMOCHistory( path );

TorrentUtil.setMOC( dms, path );
};

MenuItem set_item = new MenuItem( moc_menu, SWT.PUSH);

Messages.setLanguageText( set_item, "label.set" );

set_item.addListener(SWT.Selection, new ListenerDMTask(dms) {
@Override
public void run(DownloadManager[] dms) {
TorrentUtil.setMOC(parentShell, dms);
TorrentUtil.selectMOC(parentShell, dms, moc_setter );
}
});

set_item.setEnabled( f_canSetMOC );

if ( f_canSetMOC ){

MenuBuildUtils.addMOCHistory( moc_menu, moc_setter );
}
}
});
}
Expand Down
85 changes: 74 additions & 11 deletions uis/src/com/biglybt/ui/swt/TorrentUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,23 @@ public void run(DownloadManager[] dms) {

moc_item.setMenu( moc_menu );

// existing

String existing_moc = TorrentUtil.getMOC( dms );

if ( existing_moc != null ){

MenuItem existing_item = new MenuItem( moc_menu, SWT.PUSH );

existing_item.setText( "[" + existing_moc + "]" );

existing_item.setEnabled( false );

new MenuItem( moc_menu, SWT.SEPARATOR );
}

// clear

MenuItem clear_item = new MenuItem( moc_menu, SWT.PUSH);

Messages.setLanguageText( clear_item, "Button.clear" );
Expand All @@ -991,19 +1008,33 @@ public void run(DownloadManager[] dms) {

clear_item.setEnabled( canClearMOC );

// set

Consumer<String> moc_setter = (path)->{

MenuBuildUtils.addToMOCHistory( path );

TorrentUtil.setMOC( dms, path );
};

MenuItem set_item = new MenuItem( moc_menu, SWT.PUSH);

Messages.setLanguageText( set_item, "label.set" );

set_item.addListener(SWT.Selection, new ListenerDMTask(dms) {
@Override
public void run(DownloadManager[] dms) {
setMOC(shell, dms);
selectMOC( shell, dms, moc_setter );
}
});

set_item.setEnabled( canSetMOC );

if ( canSetMOC ){

MenuBuildUtils.addMOCHistory( moc_menu, moc_setter );
}

moc_item.setEnabled( canClearMOC || canSetMOC );

// file export
Expand Down Expand Up @@ -3653,7 +3684,44 @@ protected static void clearMOC(DownloadManager[] dms) {
}
}

protected static void setMOC(Shell shell, DownloadManager[] dms) {
protected static String getMOC(DownloadManager[] dms) {
String existing_moc = null;

if (dms != null && dms.length > 0) {

for (int i = 0; i < dms.length; i++) {

String moc_str = dms[i].getDownloadState().getAttribute( DownloadManagerState.AT_MOVE_ON_COMPLETE_DIR );

if ( existing_moc == null || existing_moc.equals( moc_str )){

existing_moc = moc_str;

}else{

existing_moc = null;

break;
}
}
}

return( existing_moc );
}

protected static void setMOC( DownloadManager[] dms, String path ) {
if (dms != null && dms.length > 0) {

path = new File( path ).getAbsolutePath();

for (int i = 0; i < dms.length; i++) {

dms[i].getDownloadState().setAttribute( DownloadManagerState.AT_MOVE_ON_COMPLETE_DIR, path );
}
}
}

protected static void selectMOC(Shell shell, DownloadManager[] dms, Consumer<String> moc_setter ) {
if (dms != null && dms.length > 0) {

DirectoryDialog dd = new DirectoryDialog(shell);
Expand All @@ -3673,15 +3741,10 @@ protected static void setMOC(Shell shell, DownloadManager[] dms) {
String path = dd.open();

if ( path != null ){

TorrentOpener.setFilterPathData(path);

File target = new File(path);

for (int i = 0; i < dms.length; i++) {

dms[i].getDownloadState().setAttribute( DownloadManagerState.AT_MOVE_ON_COMPLETE_DIR, target.getAbsolutePath());
}

TorrentOpener.setFilterPathData( path );

moc_setter.accept( path );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5206,6 +5206,8 @@ public void mouseDown(MouseEvent event) {
}
}

// existing

if ( existing_moc != null ){

MenuItem existing_item = new MenuItem( moc_menu, SWT.PUSH );
Expand All @@ -5217,6 +5219,8 @@ public void mouseDown(MouseEvent event) {
new MenuItem( moc_menu, SWT.SEPARATOR );
}

// clear

MenuItem clear_item = new MenuItem( moc_menu, SWT.PUSH);

Messages.setLanguageText( clear_item, "Button.clear" );
Expand All @@ -5232,25 +5236,13 @@ public void handleEvent(Event arg0){
cmbDataDirChanged();
}});

clear_item.setEnabled( has_moc );
clear_item.setEnabled( has_moc );

MenuItem set_item = new MenuItem( moc_menu, SWT.PUSH);

Messages.setLanguageText( set_item, "label.set" );
// set

Consumer<String> moc_setter = (path)->{
List<String> moc_hist = COConfigurationManager.getStringListParameter( "open.torrent.window.moc.history" );

moc_hist.remove( path );

moc_hist.add( 0, path );

if ( moc_hist.size() > 3 ){

moc_hist.remove( moc_hist.size()-1 );
}

COConfigurationManager.setParameter( "open.torrent.window.moc.history", moc_hist );
MenuBuildUtils.addToMOCHistory( path );

TorrentOpener.setFilterPathData(path);

Expand All @@ -5264,6 +5256,10 @@ public void handleEvent(Event arg0){
cmbDataDirChanged();
};

MenuItem set_item = new MenuItem( moc_menu, SWT.PUSH);

Messages.setLanguageText( set_item, "label.set" );

set_item.addListener(SWT.Selection, new Listener(){
@Override
public void handleEvent(Event arg0){
Expand All @@ -5283,21 +5279,7 @@ public void handleEvent(Event arg0){
}
}});

List<String> moc_hist = COConfigurationManager.getStringListParameter( "open.torrent.window.moc.history" );

if ( !moc_hist.isEmpty()){

new MenuItem( moc_menu, SWT.SEPARATOR );

for ( String hist: moc_hist ){

MenuItem hist_item = new MenuItem( moc_menu, SWT.PUSH );

hist_item.setText( hist );

hist_item.addListener( SWT.Selection, (ev)->moc_setter.accept( hist ));
}
}
MenuBuildUtils.addMOCHistory( moc_menu, moc_setter );
}

@Override
Expand Down
Loading

0 comments on commit c9bac3c

Please sign in to comment.