[Kde-java] 'Georges Interesting Literature Trawler' - code review

Richard Dale kde-java@kde.org
Sat, 13 Apr 2002 19:18:29 +0100


--------------Boundary-00=_TUQI9QSAE7X6VO9PJTUK
Content-Type: text/plain;
  charset="iso-8859-1"
Content-Transfer-Encoding: 8bit

I've just looked at George's code for the etext reader, here's some help I 
hope. There is a comment in the code 'Find why about menu doesn't work':

    // TODO Find why about data doesn't work
    KAboutData aboutData = new KAboutData("gilt","","0.3",
					  "Etext index display",
					  KAboutData.License_GPL,
					  "(c) 2002, George Russell");
    aboutData.addAuthor("George Russell", null, "george.russell@clara.net");
    KCmdLineArgs.init(cmdLineArgs,aboutData);
    KApplication app = new KApplication(cmdLineArgs,"Georges Interesting 
Literature Trawler");

The code needs to be changed to this:

    // TODO Find why about data doesn't work
    KAboutData aboutData = new KAboutData("gilt","Georges Interesting 
Literature Trawler","0.3",
					  "Etext index display",
					  KAboutData.License_GPL,
					  "(c) 2002, George Russell");
    aboutData.addAuthor("George Russell", null, "george.russell@clara.net");
    KCmdLineArgs.init(cmdLineArgs,aboutData);
    KApplication app = new KApplication();

If you use KAboutData and KCmdLineArgs, you don't need to pass any arguments 
to the KApplication constructor.

This code to set up the menu items:

    file = new QPopupMenu();
    file.insertItem("&Exit",this,SLOT("slotFileQuit()"));
    help = helpMenu("Gilt\n by George Russell");

    menu = menuBar();
    menu.insertItem("&File",file);
    menu.insertItem("&Help",help);

Needs to be changed to this:

    fileQuit = KStdAction.quit(this, SLOT("slotFileQuit()"), 
actionCollection());
    createGUI();

Then the proper KDE about menus under 'Help' will be displayed correctly, as 
well as the File->Quit option.

I think equivalent C++ code would behave exactly the same way.

-- Richard

--------------Boundary-00=_TUQI9QSAE7X6VO9PJTUK
Content-Type: text/x-java;
  charset="iso-8859-1";
  name="KSimpleBrowser.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="KSimpleBrowser.java"

import org.kde.qt.*;
import org.kde.koala.*;
import java.util.Vector;

/**
 *  Class KSimpleBrowser is the main application window.
 *
 * @see KMainWindow
 * @see KApplication
 * @see KHTMLPart
 *
 * @author java translation Kenneth J. Pouncey, kjpou@hotmail.com
 * @version 0.1
 * Modified for use in Gilt .3
 * George Russell
 */

public class KSimpleBrowser extends KMainWindow {

  MyKHTMLPart khtmlpart;
  KApplication kapp;
  KStandardDirs kdeDir = new KStandardDirs();

  Parse p = new Parse();
  Vector years, titles, files;

  private KAction fileQuit;
  QMenuBar menu;
  QPopupMenu file, help;

  String pathToIcons;

  public KSimpleBrowser (String name) {
    super(null,name,0);
    String iconDir = kdeDir.kde_default("icon");
    String prefix = kdeDir.findResourceDir("lib", "libkdecore.la");
    prefix = prefix.substring(0,prefix.indexOf("lib")-1);
    pathToIcons = prefix+"/"+iconDir+"hicolor/16x16/actions/";
    System.out.println("Icons from.. "+pathToIcons);
  
    fileQuit = KStdAction.quit(this, SLOT("slotFileQuit()"), actionCollection());
    createGUI();

    String tableStart = "<TABLE WIDTH=\"100%\">";
    String caption = "<TR><TH COLSPAN=2>Click on Etext links to download in Konqueror</TH></TR>";
    String col2b = "<TD VALIGN=\"MIDDLE\">";
    String rowStart2 = "<TR><TD VALIGN=\"MIDDLE\">";
    String rowEnd = "</TR>";
    String tableEnd = " </TABLE>";
    String linkStart = "<A HREF=\"";
    String linkEnd = "</A>";
    boolean rowAB=false;
    
// Query KDE's colour settings
    QColor baseColor = KGlobalSettings.baseColor();
    QColor altBackground = KGlobalSettings.alternateBackgroundColor();
    QColor linkColor = KGlobalSettings.linkColor();
    QColor textColor = KGlobalSettings.textColor();


    String one = Integer.toHexString(baseColor.red());
    String two = Integer.toHexString(baseColor.green()) ; 
    String three = Integer.toHexString(baseColor.blue());
    if (one.length() == 1)
      one+=one;
    if (two.length() == 1)
      two+=two;
    if (three.length() == 1)
      three += three;
    String baseColHex = one + two + three;

    one = Integer.toHexString(altBackground.red());
    two = Integer.toHexString(altBackground.green()) ; 
    three = Integer.toHexString(altBackground.blue());
    if (one.length() == 1)
      one+=one;
    if (two.length() == 1)
      two+=two;
    if (three.length() == 1)
      three += three;
    String altColHex = one + two + three;

    one = Integer.toHexString(linkColor.red());
    two = Integer.toHexString(linkColor.green()) ; 
    three = Integer.toHexString(linkColor.blue());
    if (one.length() == 1)
      one+=one;
    if (two.length() == 1)
      two+=two;
    if (three.length() == 1)
      three += three;
    String linkColHex = one + two + three;

    one = Integer.toHexString(textColor.red());
    two = Integer.toHexString(textColor.green()) ; 
    three = Integer.toHexString(textColor.blue());
    if (one.length() == 1)
      one+=one;
    if (two.length() == 1)
      two+=two;
    if (three.length() == 1)
      three += three;
    String textColHex = one + two + three;

    String beginPage = "<HTML><HEAD></HEAD><BODY fgcolor=#"+textColHex+" bgcolor=#"+baseColHex+" text=#"+textColHex+" link=#"+linkColHex+ ">";

    String rowStart1 = "<TR><TD VALIGN=\"MIDDLE\" BGCOLOR=#"+altColHex+">";
    String col2a = "<TD VALIGN=\"MIDDLE\" BGCOLOR=#"+altColHex+">";

    System.out.println("BASE "+baseColHex);
    System.out.println("ALT "+altColHex);
    System.out.println("LINK "+linkColHex);
    System.out.println("TEXT "+textColHex);
// TODO Make this selectable
    p.setInputFile("GUTINDEX.ALL");

    p.parseFile();
    years = p.getYearVector();
    titles = p.getEntries();
    files = p.getFileVector();


    khtmlpart = new MyKHTMLPart(this);

    khtmlpart.begin();
    khtmlpart.write(beginPage);
    khtmlpart.write("<P>Please report Etext links that are broken using the EMail links on the right hand side. Thanks.");
    khtmlpart.write(tableStart);
    khtmlpart.write(caption);
    StringBuffer line = new StringBuffer();
    try {
      for (int i= 0 ; i < files.size() ; i++){

	if (rowAB)
	  line.append(rowStart1);
	else
	  line.append(rowStart2);
	rowAB = !rowAB;

	if (i % 100 == 0) {
	  System.out.print('#');
	}
	line.append(linkStart);
	try {
	  line.append(
	    (p.URLGen(Integer.parseInt((String)years.elementAt(i)),
		      (String)files.elementAt(i))).toString()
	    );
	}
	catch (Exception d) {
	  System.err.println(d.toString());
	}
	line.append("\">");
	
	line.append("<IMG SRC=\"file:"+pathToIcons+"fileopen.png\">"+"</IMG>");
	line.append((String)titles.elementAt(i));
	line.append(linkEnd);

	if (!rowAB)
	  line.append(col2a);
	else
	  line.append(col2b);
	line.append(linkEnd);
	line.append(linkStart);
	line.append("etext://");
	line.append(files.size()-i);
	line.append("\">");
	line.append("<IMG SRC=\"file:"+pathToIcons+"mail_forward.png\">"+"</IMG>");
	line.append(files.size()-i);
	line.append(linkEnd);
	line.append(rowEnd);
	khtmlpart.write(line.toString());
	line.delete(0,line.length());
      }
    } catch (Exception e) {khtmlpart.write( " Caught "+e.toString()); }
    khtmlpart.write(tableEnd);
    khtmlpart.write("</BODY></HTML>");
    
    khtmlpart.end();
    BrowserExtension s = khtmlpart.browserExtension();

    connect(s, SIGNAL("openURLRequest(KURL,URLArgs)"),this,SLOT("slotNewURL(KURL)"));
    setCentralWidget(khtmlpart.view());

  }

  public void setApp(KApplication a) {
    kapp =a;
  }

  public void slotNewURL (KURL a) {
    // invoke the browser if the URL seems long enough to be genuine
    System.out.println(a.url().substring(0,5));
    if (a.url().indexOf("etext") != 0) 
      kapp.invokeBrowser(a.url());
    else {
      kapp.invokeMailer("russell@kde.org",
			"",
			"",
			" An etext link in GILT is incorrect",
			"The incorrect etext is number "+a.url()); {

      }
      System.out.println("Etext");
    }
    System.out.println("Clicked! for URL "+ a.url());
  }

  public void slotFileQuit() {
    System.out.println("Quitting");
    System.exit(0);
  }

  public static void main(String[] cmdLineArgs) {
    // TODO Find why about data doesn't work
    KAboutData aboutData = new KAboutData("gilt","Georges Interesting Literature Trawler","0.3",
					  "Etext index display",
					  KAboutData.License_GPL,
					  "(c) 2002, George Russell");
    aboutData.addAuthor("George Russell", null, "george.russell@clara.net");
    KCmdLineArgs.init(cmdLineArgs,aboutData);
    KApplication app = new KApplication();
    KSimpleBrowser kbrowser = new KSimpleBrowser("Select an Etext");
    kbrowser.setApp(app);
    app.setMainWidget(kbrowser);
    kbrowser.setCaption("Georges Interesting Literature Trawler");
    kbrowser.resize(600,600);
    kbrowser.show();
  
    app.exec();
    return;
  }
  
  static {
    qtjava.initialize();
    kdejava.initialize();
  }
}















--------------Boundary-00=_TUQI9QSAE7X6VO9PJTUK--