📄 qpeapplication.cpp
字号:
/************************************************************************ Copyright (C) 2000-2002 Trolltech AS. All rights reserved.**** This file is part of the Qtopia Environment.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#define QTOPIA_INTERNAL_LANGLIST#ifndef QTOPIA_INTERNAL_FILEOPERATIONS#define QTOPIA_INTERNAL_FILEOPERATIONS#endif#include "custom.h"#include <stdlib.h>#include <qfile.h>#ifdef QTOPIA_DESKTOP# include <qsettings.h>#endif#ifdef Q_WS_QWS# ifndef QT_NO_COP# if QT_VERSION <= 231# define private public# define sendLocally processEvent# include "qcopenvelope_qws.h"# undef private# else# include "qcopenvelope_qws.h"# endif# endif# include <qwindowsystem_qws.h>#endif#include <qtextstream.h>#include <qpalette.h>#include <qbuffer.h>#include <qptrdict.h>#include <qregexp.h>#include <qdir.h>#include <qlabel.h>#include <qdialog.h>#include <qdragobject.h>#include <qtextcodec.h>#include <qevent.h>#include <qtooltip.h>#include <qsignal.h>#include <qclipboard.h>#include <qtimer.h>#include <qpixmapcache.h>#ifdef Q_WS_QWS#include <qwsdisplay_qws.h>#endif#if defined(Q_WS_QWS) && !defined(QT_NO_COP)#define QTOPIA_INTERNAL_INITAPP#endif#include "qpeapplication.h"#if QT_VERSION >= 0x030000# include <qstylefactory.h>#else# include <qwindowsstyle.h>#endif#include "global.h"#if QT_VERSION <= 230 && defined(QT_NO_CODECS)# include "qutfcodec.h"#endif#include "config.h"#include "network.h"# include "applnk.h"#ifdef Q_WS_QWS# include "fontmanager.h"# include "fontdatabase.h"# include "power.h"# include "qpemenubar.h"# include "imagecodecinterface.h"# include "textcodecinterface.h"# include "styleinterface.h"# include "resource.h"# include "qpestyle.h"# include "qpedecoration_p.h"#endif#include "alarmserver.h"#include "pluginloader_p.h"#include <stdlib.h>#ifdef Q_OS_UNIX#include <unistd.h>#include <sys/file.h>#endif#ifdef Q_OS_WIN32#include <windows.h>#endif#ifndef QTOPIA_DESKTOP#ifndef Q_OS_WIN32#include <sys/ioctl.h>#include <sys/soundcard.h>#else#include <winbase.h>#include <mmsystem.h>#include <io.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#endif#endif#include "../qtopia1/qpe_show_dialog.cpp"class HackWidget : public QWidget{public: bool needsOk() { return (getWState() & WState_Reserved1 ); } QRect normalGeometry() { return topData()->normalGeometry; };};#ifdef Q_WS_QWSextern Q_EXPORT QRect qt_maxWindowRect;#endifclass QPEApplicationData {public: QPEApplicationData() : presstimer(0), presswidget(0), rightpressed(FALSE), kbgrabber(0), kbregrab(FALSE), notbusysent(FALSE), preloaded(FALSE), forceshow(FALSE), nomaximize(FALSE), qpe_main_widget(0), keep_running(TRUE), skiptimechanged(FALSE), execCalled(FALSE)#ifdef Q_WS_QWS ,styleLoader("styles"), styleIface(0), textPluginLoader("textcodecs"), // No tr imagePluginLoader("imagecodecs")#endif { qcopq.setAutoDelete(TRUE); } int presstimer; QWidget* presswidget; QPoint presspos; bool rightpressed; int kbgrabber; bool kbregrab; bool notbusysent; QString appName; struct QCopRec { QCopRec(const QCString &ch, const QCString &msg, const QByteArray &d) : channel(ch), message(msg), data(d) { channel.detach(); message.detach(); data.detach(); } QCString channel; QCString message; QByteArray data; }; bool preloaded; bool forceshow; bool nomaximize; QWidget* qpe_main_widget; bool keep_running; int skiptimechanged; bool execCalled; QList<QCopRec> qcopq; void enqueueQCop(const QCString &ch, const QCString &msg, const QByteArray &data) { qcopq.append(new QCopRec(ch,msg,data)); } void sendQCopQ() { if ( execCalled ) {#ifndef QT_NO_COP QCopRec* r; for (QListIterator<QCopRec> it(qcopq); (r=it.current()); ++it) QCopChannel::sendLocally(r->channel,r->message,r->data);#endif qcopq.clear(); } } static void show_mx(QWidget* mw, bool nomaximize, QString &strName) { if ( mw->isVisible() ) { mw->raise(); } else { QPoint p; QSize s; if ( mw->layout() && mw->inherits("QDialog") ) { bool max; if ( read_widget_rect(strName, max, p, s) && validate_widget_size(mw, p, s) ) { mw->resize(s); mw->move(p); if ( max && !nomaximize ) mw->showMaximized(); else mw->show(); } else { qpe_show_dialog((QDialog*)mw,nomaximize); } } else { bool max; if ( read_widget_rect(strName, max, p, s) && validate_widget_size(mw, p, s) ) { mw->resize(s); mw->move(p); } else { //no stored rectangle, make an estimation int x = (qApp->desktop()->width()-mw->frameGeometry().width())/2; int y = (qApp->desktop()->height()-mw->frameGeometry().height())/2; mw->move( QMAX(x,0), QMAX(y,0) );#ifdef Q_WS_QWS if ( !nomaximize ) mw->showMaximized();#endif } if ( max && !nomaximize ) mw->showMaximized(); else mw->show(); } } } static bool read_widget_rect(const QString &app, bool &maximized, QPoint &p, QSize &s) { maximized = TRUE; // 350 is the trigger in qwsdefaultdecoration for providing a resize button if ( qApp->desktop()->width() <= 350 ) return FALSE; Config cfg( "qpe" ); cfg.setGroup("ApplicationPositions"); QString str = cfg.readEntry( app, QString::null ); QStringList l = QStringList::split(",", str); if ( l.count() == 5) { p.setX( l[0].toInt() ); p.setY( l[1].toInt() ); s.setWidth( l[2].toInt() ); s.setHeight( l[3].toInt() ); maximized = l[4].toInt(); return TRUE; } return FALSE; } static bool validate_widget_size(const QWidget *w, QPoint &p, QSize &s) {#ifndef Q_WS_QWS QRect qt_maxWindowRect = qApp->desktop()->geometry();#endif int maxX = qt_maxWindowRect.width(); int maxY = qt_maxWindowRect.height(); int wWidth = s.width() + ( w->frameGeometry().width() - w->geometry().width() ); int wHeight = s.height() + ( w->frameGeometry().height() - w->geometry().height() ); // total window size is not allowed to be larger than desktop window size if ( ( wWidth >= maxX ) && ( wHeight >= maxY ) ) return FALSE; if ( wWidth > maxX ) { s.setWidth( maxX - (w->frameGeometry().width() - w->geometry().width() ) ); wWidth = maxX; } if ( wHeight > maxY ) { s.setHeight( maxY - (w->frameGeometry().height() - w->geometry().height() ) ); wHeight = maxY; } // any smaller than this and the maximize/close/help buttons will be overlapping if ( wWidth < 80 || wHeight < 60 ) return FALSE; if ( p.x() < 0 ) p.setX(0); if ( p.y() < 0 ) p.setY(0); if ( p.x() + wWidth > maxX ) p.setX( maxX - wWidth ); if ( p.y() + wHeight > maxY ) p.setY( maxY - wHeight ); return TRUE; } static void store_widget_rect(QWidget *w, QString &app) { // 350 is the trigger in qwsdefaultdecoration for providing a resize button if ( qApp->desktop()->width() <= 350 ) return; // we use these to map the offset of geometry and pos. ( we can only use normalGeometry to // get the non-maximized version, so we have to do it the hard way ) int offsetX = w->x() - w->geometry().left(); int offsetY = w->y() - w->geometry().top(); QRect r; if ( w->isMaximized() ) r = ( (HackWidget *) w)->normalGeometry(); else r = w->geometry(); // Stores the window placement as pos(), size() (due to the offset mapping) Config cfg( "qpe" ); cfg.setGroup("ApplicationPositions"); QString s; s.sprintf("%d,%d,%d,%d,%d", r.left() + offsetX, r.top() + offsetY, r.width(), r.height(), w->isMaximized() ); cfg.writeEntry( app, s ); } static bool setWidgetCaptionFromAppName( QWidget* /*mw*/, const QString& /*appName*/, const QString& /*appsPath*/ ) { /* // This works but disable it for now until it is safe to apply // What is does is scan the .desktop files of all the apps for // the applnk that has the corresponding argv[0] as this program // then it uses the name stored in the .desktop file as the caption // for the main widget. This saves duplicating translations for // the app name in the program and in the .desktop files. AppLnkSet apps( appsPath ); QList<AppLnk> appsList = apps.children(); for ( QListIterator<AppLnk> it(appsList); it.current(); ++it ) { if ( (*it)->exec() == appName ) { mw->setIcon( (*it)->pixmap() ); mw->setCaption( (*it)->name() ); return TRUE; } } */ return FALSE; } void show(QWidget* mw, bool nomax) { setWidgetCaptionFromAppName( mw, appName, QPEApplication::qpeDir() + "apps" ); nomaximize = nomax; qpe_main_widget = mw;#ifndef QT_NO_COP sendQCopQ();#endif if ( preloaded ) { if(forceshow) show_mx(mw,nomax, appName); } else if ( keep_running ) { show_mx(mw,nomax, appName); } }#ifdef Q_WS_QWS void loadTextCodecs() { QStringList list = textPluginLoader.list(); QStringList::Iterator it; for ( it = list.begin(); it != list.end(); ++it ) { TextCodecInterface *iface = 0; if ( textPluginLoader.queryInterface( *it, IID_QtopiaTextCodec, (QUnknownInterface**)&iface ) == QS_OK && iface ) { QValueList<int> mibs = iface->mibEnums(); for (QValueList<int>::ConstIterator i=mibs.begin(); i!=mibs.end(); ++i) { (void)iface->createForMib(*i); // ### it exists now; need to remember if we can delete it } } } } void loadImageCodecs() { QStringList list = imagePluginLoader.list(); QStringList::Iterator it; for ( it = list.begin(); it != list.end(); ++it ) { ImageCodecInterface *iface = 0; if ( imagePluginLoader.queryInterface( *it, IID_QtopiaImageCodec, (QUnknownInterface**)&iface ) == QS_OK && iface ) { QStringList formats = iface->keys(); for (QStringList::ConstIterator i=formats.begin(); i!=formats.end(); ++i) { (void)iface->installIOHandler(*i); // ### it exists now; need to remember if we can delete it } } } }#endif#ifdef Q_WS_QWS PluginLoaderIntern styleLoader; StyleInterface *styleIface; QString styleName; PluginLoaderIntern textPluginLoader; PluginLoaderIntern imagePluginLoader;#endif QString decorationName;};class ResourceMimeFactory : public QMimeSourceFactory {public: ResourceMimeFactory() : resImage(0) {#ifdef Q_WS_QWS setFilePath( Global::helpPath() ); setExtensionType("html","text/html;charset=UTF-8");#endif } ~ResourceMimeFactory() { delete resImage; } const QMimeSource* data(const QString& abs_name) const { const QMimeSource* r = QMimeSourceFactory::data(abs_name); if ( !r ) { int sl = abs_name.length(); do { sl = abs_name.findRev('/',sl-1); QString name = sl>=0 ? abs_name.mid(sl+1) : abs_name; int dot = name.findRev('.'); if ( dot >= 0 ) name = name.left(dot);#ifdef Q_WS_QWS QImage img = Resource::loadImage(name); if ( !img.isNull() ) { ResourceMimeFactory *that = (ResourceMimeFactory*)this; delete that->resImage; that->resImage = new QImageDrag( img ); r = resImage; }#endif } while (!r && sl>0); } return r; }private: QImageDrag *resImage;};int qtopia_muted=0;#ifndef QTOPIA_DESKTOPstatic void setVolume(int t=0, int percent=-1){ switch (t) { case 0: { Config cfg("Sound"); cfg.setGroup("System"); if ( percent < 0 ) percent = cfg.readNumEntry("Volume",50); int fd = 0;#ifndef Q_OS_WIN32 if ((fd = open("/dev/mixer", O_RDWR))>=0) { // Some devices require this, O_RDONLY doesn't always work int vol = qtopia_muted ? 0 : percent; // set both channels to same volume vol |= vol << 8; ioctl(fd, MIXER_WRITE(0), &vol); ::close(fd); }#else HWAVEOUT handle; WAVEFORMATEX formatData; formatData.cbSize = sizeof(WAVEFORMATEX); formatData.wFormatTag = WAVE_FORMAT_PCM; formatData.nAvgBytesPerSec = 4 * 44000; formatData.nBlockAlign = 4; formatData.nChannels = 2; formatData.nSamplesPerSec = 44000; formatData.wBitsPerSample = 16; waveOutOpen(&handle, WAVE_MAPPER, &formatData, 0L, 0L, CALLBACK_NULL); int vol = qtopia_muted ? 0 : percent; unsigned int volume = (vol << 24) | (vol << 8); if ( waveOutSetVolume( handle, volume ) ) qDebug( "set volume of audio device failed" ); waveOutClose( handle );#endif } break; }}#endifstatic int& hack(int& i){#if QT_VERSION <= 230 && defined(QT_NO_CODECS) // These should be created, but aren't in Qt 2.3.0 (void)new QUtf8Codec; (void)new QUtf16Codec;#endif return i;}#ifdef Q_WS_QWSstatic bool forced_off = FALSE;static int curbl=-1;static int backlight(){ if ( curbl == -1 ) { // Read from config Config config( "qpe" ); config.setGroup( "Screensaver" ); curbl = config.readNumEntry("Brightness",255); } return curbl;}void qpe_setBrightness(int bright);static void setBacklight(int bright){ if ( bright == -3 ) { // Forced on forced_off = FALSE; bright = -1; } if ( forced_off && bright != -2 ) return; if ( bright == -2 ) { // Toggle between off and on bright = curbl ? 0 : -1; forced_off = !bright; } if ( bright == -1 ) { // Read from config Config config( "qpe" ); config.setGroup( "Screensaver" ); bright = config.readNumEntry("Brightness",255); } qpe_setBrightness(bright); curbl = bright;}void qpe_setBacklight(int bright) { setBacklight(bright); }#endifstatic bool dim_on = FALSE;static bool lightoff_on = FALSE;static bool networkedsuspend_on = FALSE;static bool poweredsuspend_on = FALSE;#ifdef Q_WS_QWSstatic int disable_suspend = 100;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -