mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Use smart pointers for the device object
This commit is contained in:
parent
4d575db0a4
commit
e1a133c990
@ -12,15 +12,13 @@ using namespace wpd;
|
|||||||
static void
|
static void
|
||||||
dealloc(Device* self)
|
dealloc(Device* self)
|
||||||
{
|
{
|
||||||
if (self->pnp_id != NULL) free(self->pnp_id);
|
self->pnp_id.release();
|
||||||
self->pnp_id = NULL;
|
if (self->bulk_properties) self->bulk_properties.Release();
|
||||||
|
|
||||||
if (self->bulk_properties != NULL) { self->bulk_properties->Release(); self->bulk_properties = NULL; }
|
if (self->device) {
|
||||||
|
|
||||||
if (self->device != NULL) {
|
|
||||||
Py_BEGIN_ALLOW_THREADS;
|
Py_BEGIN_ALLOW_THREADS;
|
||||||
self->device->Close(); self->device->Release();
|
self->device->Close();
|
||||||
self->device = NULL;
|
self->device.Release();
|
||||||
Py_END_ALLOW_THREADS;
|
Py_END_ALLOW_THREADS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,28 +30,21 @@ dealloc(Device* self)
|
|||||||
static int
|
static int
|
||||||
init(Device *self, PyObject *args, PyObject *kwds)
|
init(Device *self, PyObject *args, PyObject *kwds)
|
||||||
{
|
{
|
||||||
PyObject *pnp_id;
|
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
|
if (!PyArg_ParseTuple(args, "O&", py_to_wchar_no_none, &self->pnp_id)) return -1;
|
||||||
if (!PyArg_ParseTuple(args, "O", &pnp_id)) return -1;
|
self->bulk_properties.Release();
|
||||||
|
|
||||||
self->pnp_id = unicode_to_wchar(pnp_id);
|
|
||||||
if (self->pnp_id == NULL) return -1;
|
|
||||||
|
|
||||||
self->bulk_properties = NULL;
|
|
||||||
|
|
||||||
CComPtr<IPortableDeviceValues> client_information = get_client_information();
|
CComPtr<IPortableDeviceValues> client_information = get_client_information();
|
||||||
if (client_information) {
|
if (client_information) {
|
||||||
self->device = open_device(self->pnp_id, client_information);
|
self->device = open_device(self->pnp_id.ptr(), client_information);
|
||||||
if (self->device != NULL) {
|
if (self->device) {
|
||||||
self->device_information = get_device_information(self->device, &(self->bulk_properties));
|
IPortableDevicePropertiesBulk *bulk_properties = NULL;
|
||||||
if (self->device_information != NULL) {
|
self->device_information = get_device_information(self->device, &bulk_properties);
|
||||||
|
if (self->device_information) {
|
||||||
ret = 0;
|
ret = 0;
|
||||||
}
|
self->bulk_properties = bulk_properties;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,14 +10,13 @@
|
|||||||
namespace wpd {
|
namespace wpd {
|
||||||
|
|
||||||
IPortableDeviceValues *get_client_information() { // {{{
|
IPortableDeviceValues *get_client_information() { // {{{
|
||||||
IPortableDeviceValues *client_information;
|
|
||||||
HRESULT hr;
|
HRESULT hr;
|
||||||
|
|
||||||
ENSURE_WPD(NULL);
|
ENSURE_WPD(NULL);
|
||||||
|
CComPtr<IPortableDeviceValues> client_information;
|
||||||
|
|
||||||
Py_BEGIN_ALLOW_THREADS;
|
Py_BEGIN_ALLOW_THREADS;
|
||||||
hr = CoCreateInstance(CLSID_PortableDeviceValues, NULL,
|
hr = client_information.CoCreateInstance(CLSID_PortableDeviceValues, NULL, CLSCTX_INPROC_SERVER);
|
||||||
CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&client_information));
|
|
||||||
Py_END_ALLOW_THREADS;
|
Py_END_ALLOW_THREADS;
|
||||||
if (FAILED(hr)) { hresult_set_exc("Failed to create IPortableDeviceValues", hr); return NULL; }
|
if (FAILED(hr)) { hresult_set_exc("Failed to create IPortableDeviceValues", hr); return NULL; }
|
||||||
|
|
||||||
@ -43,7 +42,7 @@ IPortableDeviceValues *get_client_information() { // {{{
|
|||||||
hr = client_information->SetUnsignedIntegerValue(WPD_CLIENT_SECURITY_QUALITY_OF_SERVICE, SECURITY_IMPERSONATION);
|
hr = client_information->SetUnsignedIntegerValue(WPD_CLIENT_SECURITY_QUALITY_OF_SERVICE, SECURITY_IMPERSONATION);
|
||||||
Py_END_ALLOW_THREADS;
|
Py_END_ALLOW_THREADS;
|
||||||
if (FAILED(hr)) { hresult_set_exc("Failed to set quality of service", hr); return NULL; }
|
if (FAILED(hr)) { hresult_set_exc("Failed to set quality of service", hr); return NULL; }
|
||||||
return client_information;
|
return client_information.Detach();
|
||||||
} // }}}
|
} // }}}
|
||||||
|
|
||||||
IPortableDevice *open_device(const wchar_t *pnp_id, CComPtr<IPortableDeviceValues> &client_information) { // {{{
|
IPortableDevice *open_device(const wchar_t *pnp_id, CComPtr<IPortableDeviceValues> &client_information) { // {{{
|
||||||
@ -207,7 +206,8 @@ end:
|
|||||||
return ans;
|
return ans;
|
||||||
} // }}}
|
} // }}}
|
||||||
|
|
||||||
PyObject* get_device_information(IPortableDevice *device, IPortableDevicePropertiesBulk **pb) { // {{{
|
PyObject*
|
||||||
|
get_device_information(CComPtr<IPortableDevice> &device, IPortableDevicePropertiesBulk **pb) { // {{{
|
||||||
IPortableDeviceContent *content = NULL;
|
IPortableDeviceContent *content = NULL;
|
||||||
IPortableDeviceProperties *properties = NULL;
|
IPortableDeviceProperties *properties = NULL;
|
||||||
IPortableDevicePropertiesBulk *properties_bulk = NULL;
|
IPortableDevicePropertiesBulk *properties_bulk = NULL;
|
||||||
|
@ -40,11 +40,10 @@ extern ClientInfo client_info;
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
PyObject_HEAD
|
PyObject_HEAD
|
||||||
// Type-specific fields go here.
|
// Type-specific fields go here.
|
||||||
wchar_t *pnp_id;
|
wchar_raii pnp_id;
|
||||||
IPortableDevice *device;
|
CComPtr<IPortableDevice> device;
|
||||||
PyObject *device_information;
|
PyObject *device_information;
|
||||||
IPortableDevicePropertiesBulk *bulk_properties;
|
CComPtr<IPortableDevicePropertiesBulk> bulk_properties;
|
||||||
|
|
||||||
} Device;
|
} Device;
|
||||||
extern PyTypeObject DeviceType;
|
extern PyTypeObject DeviceType;
|
||||||
|
|
||||||
@ -56,7 +55,7 @@ int pump_waiting_messages();
|
|||||||
|
|
||||||
extern IPortableDeviceValues* get_client_information();
|
extern IPortableDeviceValues* get_client_information();
|
||||||
extern IPortableDevice* open_device(const wchar_t *pnp_id, CComPtr<IPortableDeviceValues> &client_information);
|
extern IPortableDevice* open_device(const wchar_t *pnp_id, CComPtr<IPortableDeviceValues> &client_information);
|
||||||
extern PyObject* get_device_information(IPortableDevice *device, IPortableDevicePropertiesBulk **bulk_properties);
|
extern PyObject* get_device_information(CComPtr<IPortableDevice> &device, IPortableDevicePropertiesBulk **bulk_properties);
|
||||||
extern PyObject* get_filesystem(IPortableDevice *device, const wchar_t *storage_id, IPortableDevicePropertiesBulk *bulk_properties, PyObject *callback);
|
extern PyObject* get_filesystem(IPortableDevice *device, const wchar_t *storage_id, IPortableDevicePropertiesBulk *bulk_properties, PyObject *callback);
|
||||||
extern PyObject* get_file(IPortableDevice *device, const wchar_t *object_id, PyObject *dest, PyObject *callback);
|
extern PyObject* get_file(IPortableDevice *device, const wchar_t *object_id, PyObject *dest, PyObject *callback);
|
||||||
extern PyObject* create_folder(IPortableDevice *device, const wchar_t *parent_id, const wchar_t *name);
|
extern PyObject* create_folder(IPortableDevice *device, const wchar_t *parent_id, const wchar_t *name);
|
||||||
|
@ -131,7 +131,7 @@ wpd_enumerate_devices(PyObject *self, PyObject *args) {
|
|||||||
static PyObject *
|
static PyObject *
|
||||||
wpd_device_info(PyObject *self, PyObject *args) {
|
wpd_device_info(PyObject *self, PyObject *args) {
|
||||||
PyObject *ans = NULL;
|
PyObject *ans = NULL;
|
||||||
IPortableDevice *device = NULL;
|
CComPtr<IPortableDevice> device = NULL;
|
||||||
|
|
||||||
ENSURE_WPD(NULL);
|
ENSURE_WPD(NULL);
|
||||||
|
|
||||||
@ -142,12 +142,10 @@ wpd_device_info(PyObject *self, PyObject *args) {
|
|||||||
CComPtr<IPortableDeviceValues> client_information = get_client_information();
|
CComPtr<IPortableDeviceValues> client_information = get_client_information();
|
||||||
if (client_information) {
|
if (client_information) {
|
||||||
device = open_device(pnp_id.ptr(), client_information);
|
device = open_device(pnp_id.ptr(), client_information);
|
||||||
if (device != NULL) {
|
if (device) ans = get_device_information(device, NULL);
|
||||||
ans = get_device_information(device, NULL);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (device != NULL) {device->Close(); device->Release();}
|
if (device) device->Close();
|
||||||
return ans;
|
return ans;
|
||||||
} // }}}
|
} // }}}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user