String tnumstr, fnumstr, newpriostr;
int tnumber;
DiskManagerFileInfo[] files;
String[] sections;
List args = commandLine.getArgList();
LinkedList fs,fe;
DownloadManager dm;
if( args.isEmpty() )
{
console.out.println("Torrent # required!");
return;
} else {
tnumstr = (String) args.remove(0);
}
if( args.isEmpty() )
{
console.out.println("File # required!");
return;
} else {
fnumstr = (String) args.remove(0);
}
if ((console.torrents == null) || console.torrents.isEmpty()) {
console.out.println("> Command 'prio': No torrents in list (try 'show torrents' first).");
return;
}
try {
tnumber = Integer.parseInt(tnumstr);
if ((tnumber == 0) || (tnumber > console.torrents.size())) {
console.out.println("> Command 'prio': Torrent #" + tnumber + " unknown.");
return;
}
dm = (DownloadManager) console.torrents.get(tnumber - 1);
files = dm.getDiskManagerFileInfo();
}
catch (Exception e) {
e.printStackTrace();
console.out.println("> Command 'prio': Torrent # '" + tnumstr + "' unknown.");
return;
}
if( args.isEmpty() )
{
console.out.println("> Command 'prio': missing parameter for new priority");
return;
} else {
newpriostr = (String) args.remove(0);
}
if (newpriostr.equalsIgnoreCase("normal")) {
newprio = NORMAL;
} else if (newpriostr.equalsIgnoreCase("high")) {
newprio = HIGH;
} else if (newpriostr.equalsIgnoreCase("dnd")) {
newprio = DONOTDOWNLOAD;
} else if (newpriostr.equalsIgnoreCase("del")) {
newprio = DELETE;
} else {
console.out.println("> Command 'prio': unknown priority "
+ newpriostr);
return;
}
if (fnumstr.equalsIgnoreCase("all")) {
sections = new String[1];
sections[0] = "1-"+files.length;
} else
sections = fnumstr.split(",");
fs = new LinkedList();
fe = new LinkedList();
int dash,start,end;
for (int i=0; i<sections.length; i++) {
try {
if ((dash = sections[i].indexOf('-')) != -1) {
start = Integer.parseInt(sections[i].substring(0,dash));
end = Integer.parseInt(sections[i].substring(dash+1));
} else
start = end = Integer.parseInt(sections[i]);
if ((start == 0) || (end > files.length)) {
console.out.println("> Command 'prio': Invalid file range " + sections[i]);
return;
}
if (start > end) {
console.out.println("> Command 'prio': Invalid file range '"+sections[i]+"'");
}
// -1 compensates for 0-based offsets
fs.add(new Integer(start - 1));
fe.add(new Integer(end - 1));
} catch (Exception e) {
console.out.println("> Command 'prio': File # '" + sections[i]
+ "' unknown.");
return;
}
}
// console.out.println("DM was " + dm.getState());
if ((newprio == DELETE) && (dm.getState() != DownloadManager.STATE_STOPPED)) {
try {
dm.stopIt( DownloadManager.STATE_STOPPED, false, false );
} catch (Exception e) {
console.out.println("Failed to stop torrent " + tnumber);
return;
}
}
// console.out.println("DM is " + dm.getState());
int nummod = 0;
while (fs.size() > 0) {
start = ((Integer) fs.removeFirst()).intValue();
end = ((Integer) fe.removeFirst()).intValue();
for (int i = start; i <= end; i++) {
nummod++;
// DEBUG
// console.out.println("Setting priority for file " + i + " to " + newprio);