Skip to content

Commit

Permalink
Rewritten a lot of code...
Browse files Browse the repository at this point in the history
 * Added multiple blacklist support
 * Added timers support
 * Added day of the week support
 * Added phone prefix support
  • Loading branch information
Radu Andries committed Jul 21, 2011
1 parent d083528 commit 350b272
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 116 deletions.
2 changes: 2 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
License: GPL2

A small blocker for calls

Now timers, multiple lists and phone prefix supported
205 changes: 105 additions & 100 deletions daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,23 @@
CallerXDaemon::CallerXDaemon(QObject *parent) :
QObject(parent)
{
settings=new QSettings(CONFIG_PATH,QSettings::IniFormat);
settings->beginGroup("General");
whitelistmode = settings->value("whitelistmode", false).toBool();
settings->endGroup();
settings->beginGroup("Paths");
whitelistpath = settings->value("whitelistpath", DEFAULT_WHITELIST).toString();
blacklistpath = settings->value("blacklistpath", DEFAULT_BLACKLIST).toString();
settings->endGroup();
if (whitelistmode) {
listPath = whitelistpath;
}
else {
listPath = blacklistpath;
}
syncSettings(); //Creates file if it doesn't exist
globalSettings=new QSettings(CONFIG_PATH,QSettings::IniFormat);
phonePrefix=globalSettings->value("phonePrefix",QString()).toString();
Reload();
//Start global call listener
csd=new ComNokiaCsdCallInterface(BUS_NAME,BUS_PATH,QDBusConnection::systemBus());
//Initialize dbus adaptor
new CallerxAdaptor(this);
}

CallerXDaemon::~CallerXDaemon()
{
syncSettings();
delete fileWatcher;
delete settings;
delete configWatcher;
delete globalSettings;
}

void CallerXDaemon::Start()
{
//New Call
//New Call hook
connect(csd,SIGNAL(Coming(QDBusObjectPath,QString)),this,SLOT(callScreen(QDBusObjectPath,QString)));
QDBusConnection dbusif=QDBusConnection::systemBus();
if (!dbusif.isConnected()) {
Expand All @@ -49,44 +37,18 @@ void CallerXDaemon::Start()
return;
}

QFile blocklist(listPath);
if (!blocklist.exists()) {
if (!blocklist.open(QIODevice::WriteOnly | QIODevice::Text)){
return;
}
blocklist.close();
}

fileWatcher = new QFileSystemWatcher();
fileWatcher->addPath(listPath);
loadlist();
connect(fileWatcher, SIGNAL(fileChanged(const QString&)), this, SLOT(loadlist()));
configWatcher = new QFileSystemWatcher();
configWatcher->addPath(CONFIG_PATH);
connect(configWatcher, SIGNAL(fileChanged(const QString&)), this, SLOT(loadlist()));

}

void CallerXDaemon::callScreen(const QDBusObjectPath &call,const QString &number)
{
dbusPath=call.path();
if (whitelistmode) {
//Kick'em if not in the list
bool match = false;
for (int i = 0; i < nlist.size(); ++i){
if (number.startsWith(nlist.at(i))){
break;
}
}
if(!match){
callRelease();
}
}else{
for (int i = 0; i < nlist.size(); ++i){
if (number.startsWith(nlist.at(i))){
callRelease();
break;
}
}
if(isBlocked(number)){
ComNokiaCsdCallInstanceInterface call(BUS_NAME,call.path(),QDBusConnection::systemBus());
call.Release();
}

}

void CallerXDaemon::Stop()
Expand All @@ -97,64 +59,107 @@ void CallerXDaemon::Stop()

void CallerXDaemon::Reload()
{
loadlist();
globalSettings->beginGroup("General");
QStringList lists= globalSettings->value("blocklists", QStringList()).toStringList();
globalSettings->endGroup();
//Hit me plenty...
loadBlocklists(lists);
}

void CallerXDaemon::SetWhitelistMode ( bool enabled )
void CallerXDaemon::loadBlocklists(QStringList sLists)
{
whitelistmode=enabled;
fileWatcher->removePath(listPath);
if(enabled){
listPath=whitelistpath;
}else{
listPath=blacklistpath;
foreach(QString listname,sLists){
qDebug()<<"Processing "<<listname;
globalSettings->beginGroup(listname);
QStringList *l=new QStringList();
QVariantHash *settings=new QVariantHash();
settings->insert("enabled",globalSettings->value("enabled",true));
settings->insert("external",globalSettings->value("external",QString()));
settings->insert("isWhitelist",globalSettings->value("isWhitelist",false));
settings->insert("timeStart",globalSettings->value("timeStart",QTime(0,0))); //ye' olde tricke
settings->insert("timeEnd",globalSettings->value("timeEnd",QTime(0,0)));
settings->insert("days",globalSettings->value("dayStart",QVariantList()));
if(settings->value("external").toString().isEmpty()){
QStringList storage=globalSettings->value("list",QStringList()).toStringList();
//Moving from stack to heap like a boss
foreach(QString blocked,storage){
l->append(blocked);
}
}else{
QString lstpath=settings->value("external").toString();
QFile blocklist(lstpath);
if (blocklist.exists()) {
if (!blocklist.open(QIODevice::ReadOnly | QIODevice::Text)){
QTextStream in(&blocklist);
QString line = in.readLine();
while (!line.isNull()) {
l->append(line);
line = in.readLine();
}
//This could be
//l=blocklist.readAll().split("\n");
//but looses compatibility with winnoob editors
blocklist.close();
}
}
}
lists->insert(listname,l);

globalSettings->endGroup();
}
fileWatcher->addPath(listPath);
Reload();
}

void CallerXDaemon::callRelease()
{
ComNokiaCsdCallInstanceInterface call(BUS_NAME,dbusPath,QDBusConnection::systemBus());
call.Release();
}

void CallerXDaemon::loadlist()
bool CallerXDaemon::isBlocked(QString number)
{
nlist.clear();
QFile blocklist(listPath);
if (blocklist.exists()) {
if (!blocklist.open(QIODevice::ReadOnly | QIODevice::Text)){
return;
QTime currentTime = QTime::currentTime();
QTime maxTime(23,59,59);
QDate currentDate = QDate::currentDate();
bool whitelist,tempresult,inTime;
QTime timeStart,timeEnd;
QVariantList days;
if(number.startsWith(phonePrefix)){
number.remove(0,phonePrefix.size());
}
foreach(QString list,lists->keys()){
QStringList *l=lists->value(list);
QVariantHash *s=listsSettings->value(list);
whitelist=s->value("isWhitelist").toBool();
days=s->value("dayStart").toList();
timeStart=s->value("timeStart").toTime();
timeEnd=s->value("timeEnd").toTime();
bool e=s->value("enabled").toBool();
inTime=false;
if(timeStart==timeEnd)
inTime=true;
//multidate, too drunk to do seriously
if(timeStart<timeEnd){
if(currentTime>timeStart || currentTime<timeEnd)
inTime=true;
}else{
if(currentTime>timeStart && currentTime<timeEnd)
inTime=true;
}
QTextStream in(&blocklist);
QString line = in.readLine();
while (!line.isNull()) {
if (!nlist.contains(line)) {
nlist.append(line);
//Verify if list is active. This is a monster
if(e && days.contains(currentDate.dayOfWeek()) && inTime){
if(whitelist){
foreach(QString match,*l){
if(number.startsWith(match)){
tempresult=false;
}
}
if(!tempresult)
tempresult=true;
}else{
foreach(QString match,*l){
if(number.startsWith(match) && !tempresult){
tempresult=true;
}
}
}
line = in.readLine();
}
//This could be
//nlist=blocklist.readAll().split("\n");
//but looses compatibility with winnoob editors
blocklist.close();
if(tempresult)
return true;
}
return false;
}
/**
* Synchronizes settings with rc.
*/
void CallerXDaemon::syncSettings()
{
settings->beginGroup("General");
settings->setValue("whitelistmode", whitelistmode);
settings->endGroup();
settings->beginGroup("Paths");
settings->setValue("whitelistpath", whitelistpath);
settings->setValue("blacklistpath", blacklistpath);
settings->endGroup();
settings->sync();
}


#include "daemon.moc"
21 changes: 8 additions & 13 deletions daemon/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@
#define INSTANCE_INTERFACE "com.nokia.csd.Call.Instance"

#define CONFIG_PATH "/home/user/.callerxrc"
#define DEFAULT_WHITELIST "/home/user/MyDocs/whitelist.txt"
#define DEFAULT_BLACKLIST "/home/user/MyDocs/blacklist.txt"


class CallerxAdaptor;
class CallerXDaemon : public QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.callerx.ifdbus")
Q_CLASSINFO("D-Bus Interface", "org.callerx")

public:

explicit CallerXDaemon(QObject *parent = 0);
~CallerXDaemon();

bool isBlocked(QString number);
void Start();


Expand All @@ -44,22 +43,18 @@ class CallerXDaemon : public QObject
public slots:
void Reload();
void Stop();
void SetWhitelistMode(bool enabled);

private slots:
void callScreen(const QDBusObjectPath& call, const QString& number);
void callRelease();
void loadlist();
void syncSettings();

private:
QFileSystemWatcher* fileWatcher;
QStringList nlist;
QString dbusPath, listPath, whitelistpath, blacklistpath;
bool whitelistmode;
QSettings *settings;
void loadBlocklists(QStringList lists);
QString phonePrefix;
QFileSystemWatcher* configWatcher;
QHash<QString,QStringList*> *lists;
QHash<QString,QVariantHash*> *listsSettings; //Contains list settings "external","isEnabled","timeStart","timeEnd","days", "isWhitelist"
QSettings *globalSettings;
ComNokiaCsdCallInterface *csd;
ComNokiaCsdCallInstanceInterface *csdi;
CallerxAdaptor *adaptor;
};
#endif // MAINWINDOW_H
3 changes: 0 additions & 3 deletions daemon/org.callerx.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@
<interface name="org.callerx">
<method name="Reload" />
<method name="Stop" />
<method name="SetWhitelistMode">
<arg direction="in" type="b" />
</method>
</interface>
</node>

0 comments on commit 350b272

Please sign in to comment.