summaryrefslogtreecommitdiffstats
path: root/libk3b/tools/k3bthreadwidget.cpp
blob: 103b67b851e572a4d15a449282a5267767e8cb6b (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
/* 
 *
 * $Id: k3bthreadwidget.cpp 619556 2007-01-03 17:38:12Z trueg $
 * Copyright (C) 2005 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file "COPYING" for the exact licensing terms.
 */

#include "k3bthreadwidget.h"
#include "k3bdeviceselectiondialog.h"
#include <k3bdevice.h>

#include <tqevent.h>
#include <tqapplication.h>
#include <tqwaitcondition.h>


class K3bThreadWidget::Data
{
public:
  int id;
  void* data;
  TQWaitCondition con;
};


class K3bThreadWidget::DeviceSelectionEvent : public TQCustomEvent
{
public:
  DeviceSelectionEvent( TQWidget* parent, const TQString& text, int id )
    : TQCustomEvent( TQEvent::User + 22 ),
      m_parent(parent),
      m_text(text),
      m_id(id) {
  }

  TQWidget* parent() const { return m_parent; }
  const TQString& text() const { return m_text; }
  int id() const { return m_id; }

private:
  TQWidget* m_parent;
  TQString m_text;
  int m_id;
};


K3bThreadWidget* K3bThreadWidget::s_instance = 0;


K3bThreadWidget::K3bThreadWidget()
  : TQObject(),
    m_idCounter(1)
{
  m_dataMap.setAutoDelete(true);
  s_instance = this;
}


K3bThreadWidget::~K3bThreadWidget()
{
  s_instance = 0;
}


int K3bThreadWidget::getNewId()
{
  // create new data
  Data* data = new Data;
  data->id = m_idCounter++;
  data->data = 0;
  m_dataMap.insert( data->id, data );
  return data->id;
}


void K3bThreadWidget::clearId( int id )
{
  m_dataMap.remove( id );
}


K3bThreadWidget::Data* K3bThreadWidget::data( int id )
{
  return m_dataMap[id];
}


K3bThreadWidget* K3bThreadWidget::instance()
{
  if( !s_instance )
    s_instance = new K3bThreadWidget();
  return s_instance;
}


// static
K3bDevice::Device* K3bThreadWidget::selectDevice( TQWidget* parent, 
						  const TQString& text )
{
  // request a new data set
  Data* data = K3bThreadWidget::instance()->data( K3bThreadWidget::instance()->getNewId() );

  // inform the instance about the request
  TQApplication::postEvent( K3bThreadWidget::instance(),
			   new K3bThreadWidget::DeviceSelectionEvent( parent, text, data->id ) );

  // wait for the result to be ready
  data->con.wait();

  K3bDevice::Device* dev = static_cast<K3bDevice::Device*>( data->data );

  // delete the no longer needed data
  K3bThreadWidget::instance()->clearId( data->id );

  return dev;
}


void K3bThreadWidget::customEvent( TQCustomEvent* e )
{
  if( DeviceSelectionEvent* dse = dynamic_cast<DeviceSelectionEvent*>(e) ) {
    // create dialog
    K3bDevice::Device* dev = K3bDeviceSelectionDialog::selectDevice( dse->parent(), dse->text() );

    // return it to the thread
    Data* dat = data( dse->id() );
    dat->data = static_cast<void*>( dev );

    // wake the thread
    dat->con.wakeAll();
  }
}

#include "k3bthreadwidget.moc"