summaryrefslogtreecommitdiffstats
path: root/knetworkmanager-0.8/src/knetworkmanager-wired_device_tray.cpp
blob: 7e968866043328fbc6a207c8885caa4b8ad338ee (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
160
161
162
/***************************************************************************
 *
 * knetworkmanager-wired_device_tray.cpp - A NetworkManager frontend for KDE
 *
 * Copyright (C) 2005, 2006 Novell, Inc.
 *
 * Author: Helmut Schaa       <[email protected]>, <[email protected]>
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 **************************************************************************/

// TQt includes
#include <tqevent.h>
#include <tqvbox.h>
#include <tqlayout.h>
#include <tqpushbutton.h>
#include <tqbitmap.h>
#include <tqimage.h>
#include <tqpixmap.h>
#include <tqpixmapcache.h>
#include <tqpainter.h>
#include <tqstyle.h>
#include <tqstring.h>

// KDE includes
#include <kdebug.h>
#include <klocale.h>
#include <kiconloader.h>

// TQt DBus
#include <tqdbusobjectpath.h>


// KNM includes
#include "knetworkmanager-wired_device_tray.h"
#include "knetworkmanager-wired_device.h"
#include "knetworkmanager-menuitem.h"
#include "knetworkmanager-menu_subhead.h"
#include "knetworkmanager-connection_store.h"
#include "knetworkmanager-wired_connection.h"
#include "knetworkmanager-nm_proxy.h"

#include "knetworkmanager-connection_setting_info.h"
#include "knetworkmanager-connection_setting_ipv4.h"
#include "knetworkmanager-connection_setting_wired.h"
#include "knetworkmanager-connection_settings_dialog.h"

using namespace ConnectionSettings;

class WiredDeviceTrayPrivate
{
	public:
		WiredDeviceTrayPrivate() {}
		~WiredDeviceTrayPrivate() {}

		WiredDevice* dev;
};

void WiredDeviceTray::newConnection()
{
	// create a new wired connection
	Connection* conn = new WiredConnection();

	// edit the new connection
	ConnectionSettingsDialogImpl* dlg = new ConnectionSettingsDialogImpl(conn, true, NULL, tray(), "connect_something", false, TQt::WDestructiveClose);
	dlg->show();
}


void WiredDeviceTray::addMenuItems(KPopupMenu* menu)
{
	ConnectionStore* connectionStore = ConnectionStore::getInstance();

	// device title
	Subhead* subhead = new Subhead (menu, "subhead", TQString("Wired Connection (%1)").arg(d->dev->getInterface()), SmallIcon("wired", TQIconSet::Automatic));
	menu->insertItem (subhead, -1, -1);

    // bolding subhead instead
	//menu->insertSeparator();

	if (!d->dev->getCarrier())
	{
		// no carrier -> do not show any connections
		subhead = new Subhead(menu, "subhead2", i18n("Cable disconnected"), SmallIcon("no", TQIconSet::Automatic));
		menu->insertItem(subhead, -1, -1);
	}
	else
	{
		NMProxy* nm = NMProxy::getInstance();
		Connection* active_conn = nm->getActiveConnection(d->dev);
		if (active_conn)
			kdDebug() << active_conn->getObjectPath().data() << endl;

		// get all available Connections for wired devices
		TQValueList<Connection*> connections = connectionStore->getConnections(NM_SETTING_WIRED_SETTING_NAME);
		int connectionItems = 0;
		for (TQValueList<Connection*>::iterator it = connections.begin(); it != connections.end(); ++it)
		{
			WiredConnection* wiredconn = dynamic_cast<WiredConnection*>(*it);
			if (wiredconn)
			{
				// wired connection found :)
				Info* info = wiredconn->getInfoSetting();
				IPv4* ip = wiredconn->getIPv4Setting();

				// lets create a nice name for this connection
				if (info)
				{
					TQString title = info->getName();
					if (ip)
						title += TQString(" (%1)").arg(ip->getMethod() == IPv4::METHOD_DHCP ? i18n("DHCP") : i18n("Manual IP config"));
	
					NetworkMenuItem* item = new NetworkMenuItem(d->dev, wiredconn, TQT_TQOBJECT(menu));
					connectionItems += 1;

					int id = menu->insertItem(title, item, TQT_SLOT(slotActivate()));
					menu->setItemChecked(id, ((*it) == active_conn));
				}
			}
		}

		if ( connectionItems == 0) {
			// menu->insertSeparator();
			int id = menu->insertItem(SmallIcon("filenew", TQIconSet::Automatic), i18n("Create new wired connection"), this, TQT_SLOT(newConnection()));
		}
		// bring the device down
		KAction* deactivate = tray()->actionCollection()->action("deactivate_device");
		if (deactivate)
			deactivate->plug(menu);

	}
}

WiredDeviceTray::WiredDeviceTray (WiredDevice* dev, KSystemTray * parent, const char * name )
	: DeviceTrayComponent (dev, parent, name)
{
	d = new WiredDeviceTrayPrivate();
	d->dev = dev;

	setPixmapForState(NM_DEVICE_STATE_ACTIVATED, "nm_device_wired");

}

WiredDeviceTray::~WiredDeviceTray ()
{
	delete d;
}

#include "knetworkmanager-wired_device_tray.moc"