/* GiveMeADollar.java -- periodically bug someone to give me a dollar.
   Copyright (C) 2003 Casey Marshall <rsdio@metastatic.org>

   This program is useless software; you can use and/or modify it if you
   want, but you probably shouldn't. You can copy it, modify it, print
   it onto a piece of paper and eat it, sell it for a profit, or ignore
   it completely.

   THIS PROGRAM COMES WITH NO WARRANTY OF ANY KIND. EVEN THINKING THAT
   IT WOULD IS RATHER SILLY OF YOU. THIS SOFTWARE MAY ERASE YOUR HARD
   DRIVE, DRAIN YOUR BANK ACCONT, STEAL YOUR SOCIAL SECURITY NUMBER,
   KICK YOUR DOG, POISON YOUR CHILDREN, SPEAK REALLY LOUDLY IN POLITE
   COMPANY, CAUSE A MELTDOWN, CRASH AIRPLANES, OR ANNOY ANYONE IN THE
   GENERAL VICINITY. WHO KNOWS WHAT COULD HAPPEN. YOU HAVE BEEN WARNED.

   */

package org.metastatic.crap;

import java.applet.Applet;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.Random;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

public class GiveMeADollar extends Applet {

   private static final String url = "https://www.paypal.com/xclick/business=rsdio%40metastatic.org&item_name=Making+the+web+profitable+one+panhandler+at+a+time.&amount=1.00&no_note=1&tax=0&currency_code=USD";

   private static final Insets I = new Insets(10,10,10,10);

   private Random random;
   private boolean amApplet;
   private boolean running;

   public static void main(String[] argv) {
      GiveMeADollar d = new GiveMeADollar();
      d.random = new Random();
      d.run();
   }

   public void init() {
      random = new Random();
      amApplet = true;
      run();
   }

   private void run() {
      running = true;
      final JDialog dialog = new JDialog((java.awt.Frame) null, "Give me a dollar.");
      JLabel label = new JLabel("Give me a dollar.");
      JButton ok = new JButton("Ok."), no = new JButton("No!"),
         stop = new JButton("Stop asking!");
      dialog.getContentPane().setLayout(new GridBagLayout());
      dialog.getContentPane().add(label,
         new GridBagConstraints(1, 1, 3, 1, 1.0, 1.0, GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL, I, 10, 10));
      dialog.getContentPane().add(ok,
         new GridBagConstraints(1, 2, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL, I, 10, 10));
      dialog.getContentPane().add(no,
         new GridBagConstraints(2, 2, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL, I, 10, 10));
      dialog.getContentPane().add(stop,
         new GridBagConstraints(3, 2, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL, I, 10, 10));
      dialog.pack();
      ok.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            dialog.setVisible(false);
            if (amApplet) {
               try {
                  getAppletContext().showDocument(new java.net.URL(url), "_blank");
               } catch (Exception e) { }
            } else {
               displayURL(url);
            }
         }
      });
      no.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            dialog.setVisible(false);
         }
      });
      stop.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            dialog.setVisible(false);
            running = false;
         }
      });

      while (running) {
         if (!dialog.isShowing()) {
            dialog.setVisible(true);
            centerOnScreen(dialog);
         }
         int wait = random.nextInt(2879) + 1;
         try {
            Thread.sleep(wait * 60000L);
         } catch (InterruptedException ie) {
         }
      }
   }

   private static void displayURL(String url) {
      boolean windows = isWindowsPlatform();
      String cmd = null;
      try {
         if (windows) {
            Process p = Runtime.getRuntime().exec(
               "rundll32 url.dll,FileProtocolHandler " + url);
         } else {
            Process p = Runtime.getRuntime().exec(
               "mozilla -remote openUrl (" + url + ")");
            try {
               int exitCode = p.waitFor();
               if (exitCode != 0) {
                  p = Runtime.getRuntime().exec("mozilla " + url);
               }
            } catch(InterruptedException x) {
            }
         }
      } catch(Exception x) {
      }
   }

   private static boolean isWindowsPlatform() {
      String os = System.getProperty("os.name");
      return (os != null) && (os.toLowerCase().startsWith("windows"));
   }

   private void centerOnScreen(JDialog dialog) {
      Dimension d1 = Toolkit.getDefaultToolkit().getScreenSize();
      Dimension d2 = dialog.getSize();
      dialog.setLocation(d1.width / 2 - d2.width / 2,
                         d1.height / 2 - d2.height / 2);
   }
}
