WindowManagement::WindowManagement(QObject *parent)
    : QObject(parent)
{
    WindowManagementPrivate *priv = windowManagement();
    connect(priv, SIGNAL(windowActive(QString,QRect,WId)),
            this, SIGNAL(windowActive(QString,QRect,WId)));
    connect(priv, SIGNAL(windowCaption(QString)),
            this, SIGNAL(windowCaption(QString)));
}
void CPlainWriteWindow::lookupSwordKey(CSwordKey * newKey) {
    //set the raw text to the display widget
    if (!newKey)
        return;

    /*
      Set passage of newKey to key() if they're different, otherwise we'd get
      mixed up if we look up newkey which may have a different module set.
    */
    if (key() != newKey)
        key()->setKey(newKey->key());

    if (modules().count())
        displayWidget()->setText(key()->rawText());

    setWindowTitle(windowCaption());
}
void WindowManagementPrivate::activeChanged(WId w)
{
    QString caption;
    QString appName;
    QRect rect;
    bool normal = true;

    // Fetch the window size and caption.
    if (w) {
        // Synchronize against the X display and set a null error handler.
        // This is to discard error messages if the window has gone away
        // between the time that we received the active changed message
        // and when we went looking for the window details.
        Display *dpy = QX11Info::display();
        XSync(dpy, False);
        XErrorHandler oldHandler = XSetErrorHandler(nullErrorHandler);

        // Get the window size information from XGetWindowAttributes,
        // and translate the client window's top-level position to root
        // co-ordinates using XTranslateCoordinates.
        XWindowAttributes attrs;
        int x, y;
        Window child;
        memset(&attrs, 0, sizeof(attrs));
        if (XGetWindowAttributes(dpy, (Window)w, &attrs) &&
            XTranslateCoordinates(dpy, (Window)w, attrs.root, 0, 0, &x, &y, &child)) {
            rect = QRect(x, y, attrs.width, attrs.height);
        } else {
            // Window has disappeared, so wait for next active window change.
            XSync(dpy, False);
            XSetErrorHandler(oldHandler);
            return;
        }

        // Read the window caption information.
        caption = readUtf8Property(dpy, w, netWmVisibleNameAtom, utf8StringAtom);
        if (caption.isEmpty())
            caption = readUtf8Property(dpy, w, netWmNameAtom, utf8StringAtom);
        if (caption.isEmpty()) {
            // Old-style X11 application.  Retrieve WM_NAME.
            XTextProperty textProp;
            if (XGetWMName(dpy, (Window)w, &textProp)) {
                char **list;
                int count;
                if (XTextPropertyToStringList(&textProp, &list, &count)) {
                    for (int index = 0; index < count; ++index)
                        caption += QString::fromLocal8Bit(list[index]);
                    XFreeStringList(list);
                }
            }
        }

        // Read the window type to determine if it is normal, dialog, or docked.
        Atom windowType = readAtomProperty(dpy, w, netWmWindowTypeAtom);
        Window transient = 0;
        XGetTransientForHint(dpy, (Window)w, &transient);
        if (windowType) {
            normal = (windowType == netWmWindowTypeNormalAtom);
        } else {
            // No window type, so if there is a transient-for hint,
            // it is probably a dialog, otherwise normal.
            normal = (transient != 0);
        }
        if (transient != 0 && !normal) {
            // If the window is transient, but not modal, then treat it as normal.
            if (!checkForAtomProperty(dpy, w, netWmState, netWmStateModal))
                normal = true;
        }

        // Get the class hint to determine the application's name.
        XClassHint classHint;
        if (XGetClassHint(dpy, (Window)w, &classHint)) {
            appName = QString::fromLatin1(classHint.res_name);
            XFree(classHint.res_name);
            XFree(classHint.res_class);
        }

        // Monitor the window for property changes and destroy notifications.
        if (!monitoredWindows.contains(w)) {
            monitoredWindows += w;
            if (!QWidget::find(w))  // Don't select on our own windows.
                XSelectInput(dpy, (Window)w, StructureNotifyMask | PropertyChangeMask);
        }

        // Sync again to flush errors and restore the original error handler.
        XSync(dpy, False);
        XSetErrorHandler(oldHandler);

        // If the caption is "_ignore_", we should ingore this window.
        // This is used by quicklauncher.
        if (caption == QLatin1String("_ignore_"))   // No tr
            return;
    }

    // Caption should only be emitted when a normal full-screen window
    // is made active.  Dialogs and docked apps shouldn't change it.
    if (normal) {
        if (appName != prevActive || caption != prevCaption || rect != prevRect) {
            prevActive = appName;
            prevCaption = caption;
            vs->setAttribute("Caption", caption);
            QString iconName;
            QContentId cid = QContent::execToContent(appName);
            if (cid != QContent::InvalidId) {
                QContent app(cid);
                iconName = QLatin1String(":icon/")+app.iconName();
            }
            vs->setAttribute("Icon", iconName);
            emit windowCaption(caption);
        }
    }

    // Advertise the change in active window.
    bool update = false;
    if (rect != prevRect) {
        prevRect = rect;
        vs->setAttribute("Rect", rect);
        update = true;
    }
    if (caption != prevCaption) {
        prevCaption = caption;
        vs->setAttribute("Title", caption);
        update = true;
    }
    if (update) {
        emit windowActive(caption, rect, w);
    }
}