summaryrefslogblamecommitdiffstats
path: root/src/sysInfo.cpp
blob: ddd400adf5308378a210f95a5910eca152d8daa2 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                    

                  
                            


                       
 

                                                                                                   

                                                  
                                     
 
                                                                                                   
                     
                                                                                                   
                                            
                                                                       
                                       
                                       

                                           

                                            

                                             

                            
 
                                                                                                   
                                 




                                                                                      
         

                                     
 
                                                                                                   
                                



                                                                                      
                                                      
                                             
                                                                                                         

                                                                              
                 

                      
                               
                          
 


















                                                      
 

                                                                                                   
                                                                                              
 
                   
 
#include "sysInfo.h"
#include <QString>
#include <QTime>
#include <QNetworkInterface>
#include <QVariantMap>
#include <QVariantList>
#include <QByteArray>

//static int eth0_index = 0;
// ------------------------------------------------------------------------------------------------
sysInfo::sysInfo(){
	if (debug) qDebug() << "sysInfo created.";
	// Maybe search for eth0, etc
}
// ------------------------------------------------------------------------------------------------
sysInfo::~sysInfo(){}
// ------------------------------------------------------------------------------------------------
QString sysInfo::getInfo(QString& infoName){
	if (debug) qDebug() << "sysInfo : getInfo(" << infoName << ")";
	if (infoName == QString("mac"))
		return getMACAddress();
	else if (infoName == QString("ip"))
		return getIPAddress();
	else if (infoName == QString("all"))
		return getAllInfos();
	else if (infoName == QString("json"))
		return getNames();
	// still here?
	return "info_error";
}
// ------------------------------------------------------------------------------------------------
QString sysInfo::getMACAddress(){
	/* Returns MAC address of eth0 for now. */
	QNetworkInterface qni = QNetworkInterface::interfaceFromName(QString("eth0"));
	if (!qni.isValid()){
		if (debug) qDebug() << "No interface matching \"eth0\" found.";
		return "no_eth0";
	}
	//eth0_index = qni.index();
	return qni.hardwareAddress();
}
// ------------------------------------------------------------------------------------------------
QString sysInfo::getIPAddress(){
	// Again for eth0 only at the moment.
	// TODO: this doesn't quite work yet...
	QNetworkInterface qni = QNetworkInterface::interfaceFromName(QString("eth0"));
	QList<QHostAddress> addrlist = qni.allAddresses();
	// This is does not return the right IP atm...
	foreach(QHostAddress addr, addrlist){
		if (addr.protocol() == QAbstractSocket::IPv4Protocol && addr != QHostAddress::LocalHost){
			qDebug() << "eth0: IPv4 Address: " << addr.toString();
			return addr.toString();
		}
	}
	// still here?
	qDebug() << "ip_error";
	return "ip_error";
}
QByteArray sysInfo::getNames(){

	QVariantMap foo;
	foo.insert("name", "foo");
	foo.insert("type", 123);

	QVariantMap fooo;
	fooo.insert("name", "boo");
	fooo.insert("type", 321);

	QVariantList jsonV;
	jsonV << foo << fooo;

	QJson::Serializer serializer;
	QByteArray json = serializer.serialize(jsonV);

	qDebug() << json;
	return json;

}
// ------------------------------------------------------------------------------------------------
QString sysInfo::getAllInfos(){
	QString tmp = "\"mac\":\"" + getMACAddress() + "\", \"ip\":\"" + getIPAddress() +"\"";

	return tmp;
}