#include "mabitimer.h" #include "sequence.h" #include "weather.h" #include "Settings.h" #include "alertwidget.h" #include #include #include #include #include #include #include #define WIN32_LEAN_AND_MEAN #include #define WEATHER_COUNT 12 static Sequence *gSequence; static AlertWidget *gAlertWidget; static QList *gWeather; static QMultiHash *gObjects; static Settings *gSettings; static QDateTime *gErinnTime; static QTimer *gErinnTimer; static int gOffsetSeconds; static MabiTimer *self; static int selfCount; MabiTimer::MabiTimer(QObject *parent) : QObject(parent) { setObjectName("MabiTimer"); if(selfCount) { qDebug() << "MabiTimer is already..."; } else { self = this; qDebug() << "MabiTimer begin"; } selfCount++; } MabiTimer::~MabiTimer() { selfCount--; if(selfCount == 0) { self = 0; qDebug() << "MabiTimer end"; } } bool MabiTimer::initializeSequence() { if(gSequence == 0) { gSequence = new Sequence; if(gSequence->initialize()) { return true; } delete gSequence; return false; } return true; } bool MabiTimer::initializeWeather() { if(gWeather == 0 && QFile::exists("weathercommon.xml")) { gWeather = new QList; for(int i = 1; i < WEATHER_COUNT + 1; i++) { gWeather->append(new Weather(i)); } return true; } else { return gWeather != 0; } } bool MabiTimer::initializeObjects() { if(gObjects == 0) { gObjects = new QMultiHash(); } if(gSettings == 0) { gSettings = new Settings; addManageObject(gSettings); } if(gErinnTimer == 0) { gErinnTimer = new QTimer; gErinnTimer->start(500); connect(gErinnTimer, SIGNAL(timeout()), self, SLOT(erinnTimeUpdate())); addManageObject(gErinnTimer); } if(gErinnTime == 0) { gErinnTime = new QDateTime; } if(gAlertWidget == 0) { gAlertWidget = new AlertWidget; gAlertWidget->setWindowFlags(Qt::SubWindow | gAlertWidget->windowFlags()); gAlertWidget->show(); gAlertWidget->setRadius(7); gAlertWidget->setColor(QColor(Qt::white), QColor(0, 0, 0, 192)); setTransparentEx(gAlertWidget->winId(), true); addManageObject(gAlertWidget); } return true; } void MabiTimer::uninitializeObjects() { if(gSequence) { delete gSequence; gSequence = 0; } if(gWeather) { QList::const_iterator i; for(i = gWeather->begin(); i != gWeather->end(); ++i) { delete *i; } delete gWeather; gWeather = 0; } if(gAlertWidget) { delete gAlertWidget; gAlertWidget = 0; } if(gSettings) { delete gSettings; gSettings = 0; } if(gErinnTimer) { delete gErinnTimer; gErinnTimer = 0; } if(gErinnTime) { delete gErinnTime; gErinnTime = 0; } if(gObjects) { if(gObjects->size()) { qDebug() << "objects is not zero (" << gObjects->size() << ")"; } delete gObjects; gObjects = 0; } } Sequence *MabiTimer::sequence() { return gSequence; } Weather *MabiTimer::weather(int region) { if(gWeather == 0) { return 0; } if(region < 1 || region > gWeather->size()) { return 0; } return gWeather->at(region - 1); } AlertWidget *MabiTimer::alertWidget() { return gAlertWidget; } Settings *MabiTimer::settings() { return gSettings; } QDateTime *MabiTimer::erinnTime() { return gErinnTime; } bool MabiTimer::connectTimer(const QObject *receiver, const char *member) { if(gErinnTimer == 0) { return false; } return connect(gErinnTimer, SIGNAL(timeout()), receiver, member); } bool MabiTimer::disconnectTimer(const QObject *receiver, const char *member) { if(gErinnTimer == 0) { return false; } return disconnect(gErinnTimer, SIGNAL(timeout()), receiver, member); } void MabiTimer::addManageObject(QObject *object) { addManageObject(QString::fromUtf8(object->metaObject()->className()), object); } void MabiTimer::addManageObject(QString &key, QObject *object) { if(gObjects) { connect(object, SIGNAL(destroyed(QObject*)), self, SLOT(manageObjectDistoryed(QObject*))); gObjects->insert(key, object); qDebug() << "objects(count:" << gObjects->size() << ") added item (key=" << key << ", count=" << gObjects->count(key) << ")"; } else { qDebug() << "objects is null"; } } QObject *MabiTimer::manageObject(QString &key) { if(gObjects) { return gObjects->value(key); } else { return 0; } } QObject *MabiTimer::manageObject(const char *utf8Key, int size) { return manageObject(QString::fromUtf8(utf8Key, size)); } int MabiTimer::weatherCount() { if(gWeather == 0) { return 0; } return gWeather->size(); } void MabiTimer::setOffsetSeconds(int offsetSeconds) { gOffsetSeconds = offsetSeconds; } int MabiTimer::offsetSeconds() { return gOffsetSeconds; } void MabiTimer::setTransparentEx(void *handle, bool transparent) { DWORD style = GetWindowLong((HWND)handle, GWL_EXSTYLE); if(transparent) { style = (style | WS_EX_TRANSPARENT); } else { style = (style & ~WS_EX_TRANSPARENT); } SetWindowLong((HWND)handle, GWL_EXSTYLE, style); } void MabiTimer::erinnTimeUpdate() { if(gErinnTime) { QDateTime midnight; QDateTime realTime; realTime = QDateTime::currentDateTime(); realTime = realTime.addSecs(offsetSeconds()); midnight = realTime; midnight.setTime(QTime(0, 0, 0, 0)); gErinnTime->setTime_t((realTime.toTime_t() - midnight.toTime_t()) % 2160); } } void MabiTimer::manageObjectDistoryed(QObject *object) { if(gObjects) { QHash::const_iterator i; for(i = gObjects->begin(); i != gObjects->end(); i++) { if(i.value() == object) { QString key = i.key(); gObjects->remove(key, object); qDebug() << "objects(count:" << gObjects->size() << ") removed item (key=" << key << ", count=" << gObjects->count(key) << ")"; break; } } } else { qDebug() << "objects is null (objectName:" << object->objectName() << ")"; } }