001: /*
002: * This program is free software; you can redistribute it and/or
003: * modify it under the terms of the GNU General Public License
004: * as published by the Free Software Foundation; either version 2
005: * of the License, or (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011:
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016:
017: //TODO: Having capacity passed to these methods is redundant, take it out
018: //*** (should it be in GUI dir?)
019: package net.sf.jftp.gui.tasks;
020:
021: import net.sf.jftp.JFtp;
022:
023: //***
024: import net.sf.jftp.config.*;
025:
026: import java.awt.*;
027: import java.awt.event.*;
028:
029: import java.io.*;
030:
031: import java.lang.*;
032:
033: import java.util.*;
034:
035: import javax.swing.*;
036:
037: public class LastConnections {
038: public static String SENTINEL = new String("********************");
039: private static JFtp jftp;
040:
041: //*** changed this so that JFtp object is passed to it and
042: //initialized
043: public LastConnections(JFtp jftp) {
044: this .jftp = jftp;
045:
046: //init();
047: }
048:
049: //writeToFile: This code is called when modifications are done
050: //being made (should it be private and called from inside
051: //this class?)
052: //maybe I should return a boolean value stating whether or not it
053: //succeeded
054: //SHOULD THIS BE PRIVATE?
055: //public static void writeToFile(String[] a, int capacity) {
056: public static void writeToFile(String[][] a, int capacity) {
057: try {
058: File f1 = new File(Settings.appHomeDir);
059: f1.mkdir();
060:
061: File f2 = new File(Settings.last_cons);
062: f2.createNewFile();
063:
064: FileOutputStream fos;
065: PrintStream out;
066:
067: fos = new FileOutputStream(Settings.last_cons);
068: out = new PrintStream(fos);
069:
070: //String[] lastCons = new String[capacity];
071: //lastCons = readFromFile(capacity);
072: for (int i = 0; i < capacity; i++) {
073: //out.println(a[i]);
074: int j = 0;
075: out.println(a[i][j]);
076:
077: while ((j < JFtp.CAPACITY)
078: && !(a[i][j].equals(SENTINEL))) {
079: j++;
080: out.println(a[i][j]);
081:
082: //retVal[i][j] = raf.readLine();
083: //System.out.println();
084: }
085:
086: //System.out.println(a[i]);
087: }
088:
089: /*
090: RandomAccessFile raf =
091: new RandomAccessFile(f2, "rw");
092:
093: for (int i = 0; i < capacity; i++) {
094:
095: raf.writeChars(a[i] + "\n");
096:
097:
098: }
099: */
100: jftp.updateMenuBar();
101: } catch (Exception e) {
102: e.printStackTrace();
103: }
104: }
105:
106: //writeToFile
107: public static String[][] readFromFile(int capacity) {
108: //MAKE THIS 2D
109: //String[] retVal = new String[capacity];
110: String[][] retVal = new String[capacity][JFtp.CONNECTION_DATA_LENGTH];
111:
112: //if ((f.exists()))
113: // System.out.println("not found");
114: try {
115: File f1 = new File(Settings.appHomeDir);
116: f1.mkdir();
117:
118: File f2 = new File(Settings.last_cons);
119:
120: //File f;
121: //f = init(capacity);
122: if (!f2.exists()) {
123: init(capacity);
124: }
125:
126: /*
127: FileReader fr = new FileReader(Settings.last_cons);
128:
129: BufferedReader breader = new BufferedReader(fr);
130:
131: for (int i=0; i<capacity; i++) {
132:
133: retVal[i] = breader.readLine();
134: }
135: */
136: RandomAccessFile raf = new RandomAccessFile(f2, "r");
137:
138: //BUGFIX 1.40: determine if old style of file is
139: // being used
140: //THIS MAY MAKE THIS SECTION LESS TIME-EFFICIENT
141: String[] oldValues = new String[capacity];
142: String firstSection = new String("");
143: boolean oldVersion = true;
144:
145: //System.out.println(capacity);
146: for (int i = 0; i < capacity; i++) {
147: if (capacity < JFtp.CAPACITY) {
148: oldVersion = false;
149:
150: break;
151: }
152:
153: oldValues[i] = raf.readLine();
154: firstSection = oldValues[i].substring(0, 3);
155:
156: //System.out.print(i);
157: //System.out.print(firstSection);
158: //System.out.println("end");
159: if (!(firstSection.equals("FTP"))
160: && !(firstSection.equals("SFT"))
161: && !(firstSection.equals("SMB"))
162: && !(firstSection.equals("NFS"))
163: && !(firstSection.equals("nul"))) {
164: //System.out.println("###");
165: oldVersion = false;
166: }
167:
168: if (!oldVersion) {
169: //System.out.println("!!!");
170: break;
171: }
172: }
173:
174: //for
175: raf = new RandomAccessFile(f2, "r");
176:
177: if (oldVersion) {
178: //System.out.println("old file detected");
179: for (int i = 0; i < capacity; i++) {
180: oldValues[i] = raf.readLine();
181: }
182:
183: changeFile(oldValues);
184: }
185:
186: //reset to read start of file
187: raf = new RandomAccessFile(f2, "r");
188:
189: for (int i = 0; i < capacity; i++) {
190: int j = 0;
191: retVal[i][j] = raf.readLine();
192:
193: //System.out.print("--->");
194: //System.out.println(retVal[i][j]);
195: while ((j < JFtp.CONNECTION_DATA_LENGTH)
196: && !(retVal[i][j].equals(SENTINEL))) {
197: j++;
198: retVal[i][j] = raf.readLine();
199:
200: //System.out.print("--->");
201: //System.out.println(retVal[i][j]);
202: //j++;
203: }
204:
205: //while
206: //retVal[i][j+1] = raf.readLine();
207: }
208:
209: //for
210: } catch (Exception ex) {
211: ex.printStackTrace();
212: }
213:
214: return retVal;
215: }
216:
217: //readFromFile
218: public static String[][] prepend(String[] newString, int capacity,
219: boolean newConnection) {
220: //BUGFIX: 2D
221: //String[] lastCons = new String[capacity];
222: String[][] lastCons = new String[capacity][JFtp.CONNECTION_DATA_LENGTH];
223:
224: lastCons = readFromFile(capacity);
225:
226: for (int i = 0; i < capacity; i++) {
227: int j = 0;
228:
229: while (!(lastCons[i][j].equals(SENTINEL))) {
230: //temp[j] = lastCons[i][j];
231: //lastCons[i][j] = newString[j];
232: //System.out.println(lastCons[i][j]);
233: j++;
234: }
235:
236: //System.out.println(lastCons[i][j]);
237: }
238:
239: //BUGFIX: now need array to represent 1st connection data
240: //String temp = new String("");
241: String[] temp = new String[JFtp.CONNECTION_DATA_LENGTH];
242:
243: int j = 0;
244:
245: //is temp necessary?
246: //System.out.println("++++++++++++++++++++");
247: while (!(lastCons[0][j].equals(SENTINEL))) {
248: //while (!(newString[j].equals(SENTINEL))) {
249: temp[j] = lastCons[0][j];
250:
251: //lastCons[0][j] = newString[j];
252: //System.out.println(lastCons[0][j]);
253: //if (lastCons[0][j].equals(SENTINEL))
254: //break;
255: j++;
256: }
257:
258: temp[j] = SENTINEL;
259:
260: j = 0;
261:
262: while (!(newString[j].equals(SENTINEL))) {
263: lastCons[0][j] = newString[j];
264: j++;
265: }
266:
267: lastCons[0][j] = SENTINEL;
268:
269: //take out any data that would be "left over" after sentinel
270: //is this necessary?
271: j++;
272:
273: while (j < JFtp.CONNECTION_DATA_LENGTH) {
274: lastCons[0][j] = "";
275: j++;
276: }
277:
278: //System.out.println("-----------------");
279: for (int i = 0; i < capacity; i++) {
280: //while (!(lastCons[j].equals(SENTINEL))) {
281: //newString = temp;
282: if ((i + 1) != capacity) {
283: //System.out.print("-->");
284: j = 0;
285:
286: //System.out.println(temp[j]);
287: //shift temp data into newString
288: while (!(temp[j].equals(SENTINEL))) {
289: newString[j] = temp[j];
290:
291: //System.out.println(newString[j]);
292: j++;
293: }
294:
295: newString[j] = SENTINEL;
296:
297: //System.out.println("$$$$$$$$$$$$$");
298: //store lastCons data in temp again
299: j = 0;
300:
301: //while (!(temp[j].equals(SENTINEL))) {
302: while (!(lastCons[i + 1][j].equals(SENTINEL))) {
303: //newString[j] = temp[j];
304: temp[j] = lastCons[i + 1][j];
305:
306: //temp[j] = lastCons[i + 1][j];
307: //lastCons[i+1][j] = newString[j];
308: //j++;
309: //System.out.println(lastCons[i+1][j]);
310: //System.out.println(temp[j]);
311: //if (temp[j].equals(SENTINEL))
312: //if (lastCons[i][j].equals(SENTINEL))
313: //break;
314: //temp[j] = lastCons[i+1][j];
315: j++;
316: }
317:
318: //while
319: temp[j] = SENTINEL;
320:
321: //System.out.println("+-+-+-+-+-+-");
322: //then, make adjustments to lastCons
323: j = 0;
324:
325: //while (!(lastCons[i+1][j].equals(SENTINEL))) {
326: while (!(newString[j].equals(SENTINEL))) {
327: lastCons[i + 1][j] = newString[j];
328:
329: //System.out.println(lastCons[i+1][j]);
330: j++;
331: }
332:
333: lastCons[i + 1][j] = SENTINEL;
334:
335: //j=0;
336:
337: /*
338: while (!(lastCons[i][j].equals(SENTINEL))) {
339:
340: temp[j] = lastCons[i+1][j];
341: j++;
342:
343: }
344: */
345:
346: //lastCons[i+1][j] = SENTINEL;
347: }
348:
349: //if
350: }
351:
352: //for
353: for (int i = 0; i < capacity; i++) {
354: j = 0;
355:
356: while (!lastCons[i][j].equals(SENTINEL)) {
357: //System.out.println(lastCons[i][j]);
358: if (lastCons[i][j].equals(SENTINEL)) {
359: break;
360: }
361:
362: j++;
363: }
364:
365: //System.out.println(lastCons[i][j]);
366: }
367:
368: //***
369: if (newConnection) {
370: writeToFile(lastCons, capacity);
371: }
372:
373: return lastCons;
374: }
375:
376: //prepend
377: public static String[][] moveToFront(int position, int capacity) {
378: //make these 2D
379: //String[] lastCons = new String[capacity];
380: //String[] newLastCons = new String[capacity];
381: String[][] lastCons = new String[capacity][JFtp.CONNECTION_DATA_LENGTH];
382: String[][] newLastCons = new String[capacity][JFtp.CONNECTION_DATA_LENGTH];
383:
384: //System.out.print("--------:");
385: //System.out.println(capacity);
386: lastCons = readFromFile(capacity);
387:
388: //String temp = new String("");
389: String[] temp = new String[JFtp.CONNECTION_DATA_LENGTH];
390:
391: //String[] lastCons = new String[capacity];
392: int j = 0;
393: temp[j] = lastCons[position][j];
394:
395: while (!(lastCons[position][j].equals(SENTINEL))) {
396: j++;
397: temp[j] = lastCons[position][j];
398: }
399:
400: j = 0;
401:
402: //System.out.println("START");
403: while (!(lastCons[position][j].equals(SENTINEL))) {
404: //System.out.println(lastCons[position][j]);
405: j++;
406: }
407:
408: //System.out.println("END");
409: //possible bugfix code?
410: /*
411: for (int i=0; i<JFtp.CONNECTION_DATA_LENGTH; i++) {
412: temp[i] = lastCons[position][i];
413: }
414: */
415: newLastCons = prepend(temp, position + 1, false);
416:
417: for (int i = 0; i <= position; i++) {
418: j = 0;
419:
420: //while (!(lastCons[position][i].equals(SENTINEL))) {
421: //while (!(lastCons[i][j].equals(SENTINEL))) {
422: while (!(newLastCons[i][j].equals(SENTINEL))) {
423: //j++;
424: //temp[i] = lastCons[position][i];
425: //System.out.println(i);
426: //System.out.println(j);
427: //System.out.println(newLastCons[i][j]);
428: lastCons[i][j] = newLastCons[i][j];
429: j++;
430: }
431:
432: lastCons[i][j] = SENTINEL;
433: }
434:
435: //for
436: writeToFile(lastCons, capacity);
437:
438: return lastCons;
439: }
440:
441: //moveToFront
442: public static int findString(String[] findVal, int capacity) {
443: //BUGFIX: 2D
444: //String[] lastCons = new String[capacity];
445: String[][] lastCons = new String[capacity][JFtp.CONNECTION_DATA_LENGTH];
446:
447: lastCons = readFromFile(capacity);
448:
449: for (int i = 0; i < capacity; i++) {
450: int j = 0;
451:
452: while ((j < JFtp.CAPACITY)
453: && findVal[j].equals(lastCons[i][j])
454: && !(lastCons[i][j].equals(SENTINEL))
455: && !(findVal[j].equals(SENTINEL))) {
456: //System.out.println("start ");
457: //System.out.print(lastCons[i][j]);
458: //System.out.print(findVal[j]);
459: //System.out.println("end");
460: j++;
461:
462: //if (findVal.equals(lastCons[i]))
463: //return i;
464: }
465:
466: if (findVal[j].equals(lastCons[i][j])) {
467: //System.out.println("test");
468: //System.out.println(lastCons[i][j]);
469: //System.out.println(findVal[j]);
470: return i;
471: } else {
472: //System.out.println("test2");
473: //System.out.println(lastCons[i][j]);
474: //System.out.println(findVal[j]);
475: //System.out.println("NO");
476: }
477: }
478:
479: //if not found, return -1
480: return -1;
481: }
482:
483: //findString
484:
485: /*
486: //unnecessary?
487: public static String[] swap(String a[], int pos1, int pos2) {
488:
489: String temp = new String("");
490:
491: temp = a[pos2];
492: a[pos2] = a[pos1];
493: a[pos1] = temp;
494:
495: return a;
496:
497: } //swap
498: */
499: private static void init(int capacity) {
500: //File f = new File(Settings.last_cons);
501: try {
502: FileOutputStream fos;
503: PrintStream out;
504:
505: fos = new FileOutputStream(Settings.last_cons);
506: out = new PrintStream(fos);
507:
508: for (int i = 0; i < capacity; i++) {
509: out.println("null");
510: out.println(SENTINEL);
511: }
512:
513: fos.close();
514: } catch (Exception e) {
515: e.printStackTrace();
516: }
517:
518: //return f;
519: }
520:
521: //init
522: private static void changeFile(String[] oldValues) {
523: StringTokenizer tokens;
524:
525: String[][] newData = new String[JFtp.CAPACITY][JFtp.CONNECTION_DATA_LENGTH];
526:
527: //this assumes that capacity will not change
528: //should this be set to 9 instead of JFtp.CAPACITY?
529: for (int i = 0; i < JFtp.CAPACITY; i++) {
530: //System.out.println(oldValues[i]);
531: tokens = new StringTokenizer(oldValues[i], " ");
532:
533: int j = 0;
534:
535: while ((tokens.hasMoreTokens())) {
536: newData[i][j] = tokens.nextToken();
537:
538: //System.out.println(newData[i][j]);
539: j++;
540: }
541:
542: newData[i][j] = SENTINEL;
543:
544: //System.out.print("---->");
545: //System.out.println(newData[i][0]);
546: //System.out.println(j);
547: //for backwards compatibility with versions that
548: //don't have SFTP port remembered: enter port 22
549: //(which is default) there
550: if (newData[i][0].equals("SFTP") && (j == 5)) {
551: String temp = new String("");
552: String temp2 = new String("");
553:
554: temp = newData[i][4];
555: newData[i][4] = "22";
556:
557: temp2 = newData[i][5];
558:
559: newData[i][5] = temp;
560:
561: //temp2 = newData[i][5];
562: //newData[i][6] = temp2;
563: //newData[i][7] = SENTINEL;
564: newData[i][6] = SENTINEL;
565:
566: /*
567: for (int j=5; j++; j<7) {
568:
569: temp = newData[i][j];
570: newData[i][j] = newData[i][j-1];
571:
572: } */
573: }
574:
575: //if
576: }
577:
578: //for
579: writeToFile(newData, JFtp.CAPACITY);
580: }
581:
582: //changeFile
583: }
584:
585: //LastConnections
|