summaryrefslogtreecommitdiffstats
path: root/k9devices/k9halconnection.cpp
blob: e55c0b3b994d80e9a043153bbf59ce3c95cb2d62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// C++ Implementation: k9halconnection
//
// Description: 
//
//
// Author: Jean-Michel PETIT <[email protected]>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "k9common.h"
#ifdef HAVE_HAL

#include "k9halconnection.h"
#define DBUS_API_SUBJECT_TO_CHANGE
#include "k9haldevice.h"
#include <dbus/dbus.h>
#include <hal/libhal.h>
k9HalConnection *Hinstance=NULL;

void halDeviceAdded (LibHalContext *ctx, const char *udi) {
   Hinstance->addDevice(udi);
   Hinstance->testVolumeChanged( udi);
   
}

void halDeviceRemoved (LibHalContext *ctx, const char *udi) {
  Hinstance->removeDevice( udi);
  Hinstance->testVolumeChanged( udi);
}

  
k9HalConnection::k9HalConnection(TQObject *parent, const char *name)
 : TQObject(parent, name)
{
   m_devices.setAutoDelete(true);
  m_context =(void*) libhal_ctx_new();

  DBusError error;
  dbus_error_init( &error );
  m_dbusConnect = dbus_bus_get( DBUS_BUS_SYSTEM, &error );
  if( dbus_error_is_set(&error) ) {
    tqDebug("%s", (TQString("Error connecting to DBUS : %1").arg(error.message)).ascii());
    return;
  }

  m_dbusDispatch=new K9DBusDispatch(this,0);
  m_dbusDispatch->setConnection(m_dbusConnect);

  libhal_ctx_set_dbus_connection((LibHalContext*) m_context,m_dbusConnect );
  
  libhal_ctx_set_device_added( (LibHalContext*)m_context, halDeviceAdded );
  libhal_ctx_set_device_removed( (LibHalContext*)m_context, halDeviceRemoved );
  
  if( !libhal_ctx_init((LibHalContext*) m_context, 0 ) ) {
    tqDebug("HAL init failed");
    return;
  }

  int numDevices;
  char** halDeviceList = libhal_get_all_devices((LibHalContext*) m_context, &numDevices, 0 );
  for( int i = 0; i < numDevices; ++i )
    //tqDebug(halDeviceList[i]);
    addDevice( halDeviceList[i] );
}

void k9HalConnection::removeDevice( const char* udi ) {
   k9HalDevice *device=findDevice( udi);
   if (device !=NULL) {
	emit deviceRemoved( device);
	m_devices.remove(device);
   }
}

void k9HalConnection::addDevice( const char* udi )
{
  // ignore devices that have no property "info.capabilities" to suppress error messages
  if( !libhal_device_property_exists( (LibHalContext*) m_context, udi, "info.capabilities", 0 ) )
    return;

  if( libhal_device_query_capability( (LibHalContext*) m_context, udi, "storage.cdrom", 0 ) ) {
    char* dev = libhal_device_get_property_string( (LibHalContext*) m_context, udi, "block.device", 0 );
    if( dev ) {
      TQString s( dev );
      libhal_free_string( dev );

      if( !s.isEmpty() ) {
	k9HalDevice *device=new k9HalDevice(this,udi);
	m_devices.append( device);
	emit deviceAdded( device);
      }
    }
  } 
}


void k9HalConnection::testVolumeChanged( const char * udi) {
  // ignore devices that have no property "info.capabilities" to suppress error messages
  if( !libhal_device_property_exists( (LibHalContext*) m_context, udi, "info.capabilities", 0 ) ){
    k9HalDevice *device=findDeviceByVolume( udi);
    if (device != NULL)
    	device->updateVolumeName();
  }  
  else 
  if( libhal_device_query_capability( (LibHalContext*) m_context, udi, "volume", 0 ) ) {
    char* udiParent = libhal_device_get_property_string( (LibHalContext*) m_context, udi, "info.parent", 0 );
    if( udiParent ) {
      k9HalDevice *device=findDevice( udiParent);    
      libhal_free_string( udiParent );
      if (device)
      	device->updateVolumeName();
    }
  }
 }


k9HalConnection::~k9HalConnection()
{
 libhal_ctx_shutdown((LibHalContext*)m_context, 0 );
  libhal_ctx_free ((LibHalContext*)m_context);
  #ifdef DBUS_QT3
  //TQDBusConnection::closeConnection("sbus");
  #else
  //delete m_dBusTQtConnect;
  #endif
}


k9HalConnection * k9HalConnection::getInstance() {
    if (Hinstance==NULL)
    	Hinstance=new k9HalConnection(NULL,NULL);
    return Hinstance;
}

k9HalDevice *k9HalConnection::findDevice( const char *udi) {
    for (k9HalDevice *dev= m_devices.first();dev;dev=m_devices.next()) {
        if (dev->name()==TQString(udi)) 
            return dev;
    }
    return NULL;
}

k9HalDevice *k9HalConnection::findDeviceByVolume( const char *udi) {
    for (k9HalDevice *dev= m_devices.first();dev;dev=m_devices.next()) {
        if (dev->getVolumeUdi()==TQString(udi)) 
            return dev;
    }
    return NULL;
}

void k9HalConnection::end() {
    if (Hinstance !=NULL){
    	delete Hinstance;
    }	
    Hinstance=NULL;
}
#include "k9halconnection.moc"
#endif