Missing org.freedesktop.UDisks signals (DBus 2.7 and Java)

Yellrag Yellrag at gmail.com
Fri Apr 25 03:46:33 PDT 2014


Matthew Johnson <dbus at ...> writes:

Hello,

I grabbed the code from you and Mazta as it was exactly what it needed.
I placed it in a User Interface, and I am using it to enable JCheckBoxes.

So when I run my program I got my JFrame, and 6 CheckBoxes grayed out. When
I plug a first usb disk, the console says "Device Added!" and the first
CheckBox gets enabled, all works fine. But if I either plug in a second disk
or unplug/replug the first one, the console prints "Device Added!" 4 or 5
times (depends on which USB port I plug it in) and 4 or 5 or the JCheckBoxes
are enabled.

I tried it while running udisks --monitor and udevadm monitor in a terminal
and they output like the device is plugged only once.
added:     /org/freedesktop/UDisks/devices/sdc
added:     /org/freedesktop/UDisks/devices/sdc1



Here's the code:


package copy;

import javax.swing.*;
import javax.swing.filechooser.*;

import org.freedesktop.UDisks;
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.DBusSigHandler;
import org.freedesktop.dbus.exceptions.DBusException;

import java.awt.*;
import java.awt.event.*;
import java.util.regex.*;

public class PanelGlobal extends JPanel implements ActionListener {
	
	PanelCoches PC = new PanelCoches();
        //JPanel containing the JCheckBoxes named slot1, slot2...
	
	int nbDisks = 0; //Number of plugged disks
	JCheckBox[] slots = {PC.slot1, PC.slot2, PC.slot3, PC.slot4, PC.slot5,
PC.slot6};
	
//Constructor
public PanelGlobal() throws DBusException {
		
		//GridBagLayout and other JPanel stuff
		
		
		//Disk Plugging Detection
	    final DBusConnection conn = 
	    		DBusConnection.getConnection(DBusConnection.SYSTEM);
        
//I put a handler inside of another handler, because I had the "Device
Added!" twice as when I plug a disk, sdc and sdc1 are mounted. I assume it
goes into the first handler when sdc is mounted and in the second when sdc1
is mounted. Is this outrageous and shouldn't be done?
        conn.addSigHandler(UDisks.DeviceAdded.class,
        		         new DBusSigHandler<UDisks.DeviceAdded>() {
              @Override
              public void handle(UDisks.DeviceAdded added) { 
                try{ 
                    conn.addSigHandler (UDisks.DeviceAdded.class, 
                                 new DBusSigHandler<UDisks.DeviceAdded>() {
			@Override
			public void handle(UDisks.DeviceAdded added) { 
			System.out.println("Disk Connected");
			slots[nbDisks].setEnabled(true);
			 nbDisks++;
			}
		    });
		} catch (DBusException e) {
                // Nothing Done If Exception
		}
              }
        }); //End of first handler
		
		
}

}




More information about the dbus mailing list