File indexing completed on 2024-05-12 15:37:55

0001 /*
0002  *  This file is part of the KDE libraries
0003  *  Copyright (C) 2006 Matt Broadstone (mbroadst@gmail.com)
0004  *  Copyright (C) 2008 Maks Orlovich (maksim@kde.org)
0005  *
0006  *  This library is free software; you can redistribute it and/or
0007  *  modify it under the terms of the GNU Library General Public
0008  *  License as published by the Free Software Foundation; either
0009  *  version 2 of the License, or (at your option) any later version.
0010  *
0011  *  This library is distributed in the hope that it will be useful,
0012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  *  Library General Public License for more details.
0015  *
0016  *  You should have received a copy of the GNU Library General Public
0017  *  License along with this library; if not, write to the Free Software
0018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0019  */
0020 
0021 #include "scriptsdock.h"
0022 
0023 #include <QVBoxLayout>
0024 #include <QTreeWidget>
0025 #include <QTreeWidgetItem>
0026 #include <QStandardItemModel>
0027 #include <QHeaderView>
0028 #include <QUrl>
0029 
0030 #include "debugwindow.h"
0031 #include "debugdocument.h"
0032 
0033 using namespace KJS;
0034 using namespace KJSDebugger;
0035 
0036 ScriptsDock::ScriptsDock(QWidget *parent)
0037     : QDockWidget(i18n("Loaded Scripts"), parent)
0038 {
0039     setFeatures(DockWidgetMovable | DockWidgetFloatable);
0040     m_widget = new QTreeWidget;
0041     m_widget->header()->hide();
0042 
0043     connect(m_widget, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
0044             this, SLOT(scriptSelected(QTreeWidgetItem*,int)));
0045 
0046     setWidget(m_widget);
0047 }
0048 
0049 ScriptsDock::~ScriptsDock()
0050 {
0051 }
0052 
0053 void ScriptsDock::documentDestroyed(DebugDocument *document)
0054 {
0055     // We may be asked to remove a document held for reload repeatedly;
0056     // ignore later attempts
0057     if (!m_documents.contains(document)) {
0058         return;
0059     }
0060 
0061     QTreeWidgetItem *fragment = m_documents[document];
0062     m_documents.remove(document);
0063 
0064     QTreeWidgetItem *file      = fragment->parent();
0065     QTreeWidgetItem *domain    = file->parent();
0066 
0067     delete file->takeChild(file->indexOfChild(fragment));
0068 
0069     if (file->childCount()) {
0070         return;
0071     }
0072 
0073     // Need to clean up the file as well.
0074     delete domain->takeChild(domain->indexOfChild(file));
0075 
0076     if (domain->childCount()) {
0077         return;
0078     }
0079 
0080     // ... and domain
0081     m_headers.remove(domain->text(0));
0082     delete m_widget->takeTopLevelItem(m_widget->indexOfTopLevelItem(domain));
0083 }
0084 
0085 void ScriptsDock::addDocument(DebugDocument *document)
0086 {
0087     assert(!m_documents.contains(document));
0088 
0089     QString name = document->name();
0090     QString domain;
0091     QString favicon;
0092 
0093     if (document->url().isEmpty()) {
0094         domain = "????";    // TODO: proper i18n'able string
0095     }
0096 
0097     else {
0098         QUrl kurl(document->url());
0099         if (!kurl.host().isEmpty()) {
0100             domain = kurl.host();
0101         } else {
0102             domain = "localhost";
0103         }
0104 
0105         favicon = KIO::favIconForUrl(kurl);
0106     }
0107 
0108     QTreeWidgetItem *parent = 0;
0109     if (m_headers.contains(domain)) {
0110         parent = m_headers[domain];
0111     } else {
0112         parent = new QTreeWidgetItem(QStringList() << domain);
0113         if (!favicon.isEmpty()) {
0114             QIcon icon = QIcon::fromTheme(favicon);
0115             parent->setIcon(0, icon);
0116         }
0117 
0118         m_headers[domain] = parent;
0119         m_widget->invisibleRootItem()->addChild(parent);
0120     }
0121 
0122     // Now try to find a kid for the name
0123     QTreeWidgetItem *file = 0;
0124     for (int c = 0; c < parent->childCount(); ++c) {
0125         QTreeWidgetItem *cand = parent->child(c);
0126         if (cand->text(0) == name) {
0127             file = cand;
0128         }
0129     }
0130 
0131     if (!file) {
0132         file = new QTreeWidgetItem(parent, QStringList() << name);
0133     }
0134 
0135     // Now add the fragment
0136     QString lines = QString::number(1 + document->baseLine()) + "-" +
0137                     QString::number(1 + document->baseLine() + document->length() - 1);
0138 
0139     QTreeWidgetItem *child = new QTreeWidgetItem(file, QStringList() << lines);
0140     m_documents[document] = child;
0141 }
0142 
0143 void ScriptsDock::scriptSelected(QTreeWidgetItem *item, int /*column*/)
0144 {
0145     DebugDocument *doc = m_documents.key(item);
0146     if (doc) {
0147         emit displayScript(doc);
0148     }
0149 }
0150 
0151 #include "moc_scriptsdock.cpp"