summaryrefslogtreecommitdiffstats
path: root/quanta/dialogs/tagdialogs/tagdialog.cpp
blob: 4b491b26373acf78d43166ecfb38d82a57d28440 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/***************************************************************************
                          tagdialog.cpp  -  description
                             -------------------
    begin                : Sat Apr 1 2000
    copyright            : (C) 2000 by Yacovlev Alexander & Dmitry Poplavsky <pdima@mail.univ.kiev.ua>
                           (C) 2002-2004 by Andras Mantia <amantia@kde.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

// include qt headers
#include <tqdir.h>
#include <tqfileinfo.h>
#include <tqevent.h>

// include kde headers
#include <klocale.h>
#include <kstandarddirs.h>
#include <ktexteditor/viewcursorinterface.h>
#include <kconfig.h>
#include <kurl.h>
#include <kstdguiitem.h>
#include <kdebug.h>

#include "tagattr.h"
#include "tagdialog.h"
#include "tagwidget.h"
#include "tagimgdlg.h"
#include "tagxml.h"

#include "document.h"
#include "qextfileinfo.h"
#include "quantacommon.h"
#include "resource.h"
#include "qtag.h"
#include "kafkacommon.h"

TagDialog::TagDialog(TQTag* dtdTag, Tag *tag, KURL a_baseURL)
    : TQTabDialog( 0L, "tagdialog", true)
{
  init(dtdTag, a_baseURL);

  m_tag = tag;
  if (m_tag)
  {
    for (int i = 0; i < m_tag->attrCount(); i++)
    {
    dict->insert(m_tag->attribute(i), new TQString(m_tag->attributeValue(i)));
    }
  }
  mainDlg = 0L;
  parseTag();

}

TagDialog::TagDialog(TQTag* dtdTag, Tag *tag, const TQString& selection, KURL a_baseURL)
    : TQTabDialog( 0L, "tagdialog", true), m_selection(selection)
{
    init(dtdTag, a_baseURL);

    m_tag = tag;
    if (m_tag)
    {
        for (int i = 0; i < m_tag->attrCount(); i++)
        {
            dict->insert(m_tag->attribute(i), new TQString(m_tag->attributeValue(i)));
        }
    }
    mainDlg = 0L;
    parseTag();
}

TagDialog::TagDialog(TQTag* dtdTag, TQString attrs, KURL a_baseURL)
    : TQTabDialog( 0L, "tagdialog", true)
{
  init(dtdTag, a_baseURL);
  if ( !attrs.isNull() )
  {
    parseAttributes(attrs);
  }
  mainDlg = 0L;
  m_tag = 0L;
  parseTag();
}

TagDialog::TagDialog(TQTag* dtdTag, const TQString& selection, TQString attrs, KURL a_baseURL)
    : TQTabDialog( 0L, "tagdialog", true), m_selection(selection)
{
    init(dtdTag, a_baseURL);
    if ( !attrs.isNull() )
    {
        parseAttributes(attrs);
    }
    mainDlg = 0L;
    m_tag = 0L;
    parseTag();
}

TagDialog::~TagDialog()
{
  if (deleteTag)
  {
    delete dtdTag;
  }
  delete dict;
}


void TagDialog::init(TQTag *a_dtdTag, KURL a_baseURL)
{
  setOkButton(KStdGuiItem::ok().text());
  setCancelButton(KStdGuiItem::cancel().text());

  connect( this, TQT_SIGNAL(applyButtonPressed()),  TQT_SLOT(slotAccept()) );
  connect( this, TQT_SIGNAL(cancelButtonPressed()), TQT_SLOT(reject()) );

  if (!a_dtdTag)   //the tag is invalid, let's create a default one
  {
    dtdTag = new TQTag();
    dtdTag->setName(i18n("Unknown tag"));
    deleteTag = true;
  } else
  {
    dtdTag = a_dtdTag;
    deleteTag = false;
  }
  dict = new TQDict<TQString>(1,false);
  dict->setAutoDelete(true);
  m_baseURL = a_baseURL;

  TQString caption = i18n("Tag Properties: ");
  caption += dtdTag->name();
  setCaption( caption);
  resize(420,400);
}
/**  */
void TagDialog::parseTag()
{
  if (dtdTag->name() != i18n("Unknown tag")) //read from the extra tags
  {
    TQDomDocument doc;
    if (dtdTag->name().lower() == "img") //NOTE: HTML specific code!
    {
       mainDlg = new TagImgDlg(dtdTag->parentDTD, this);
      ((TagImgDlg *)mainDlg)->writeAttributes( dict );
    }
    //read the tag file it is available
    else if (TQFileInfo(dtdTag->fileName()).exists())
    {
       TQFile f( dtdTag->fileName() );
       f.open( IO_ReadOnly );
       if ( doc.setContent( &f ) )
       {
       TQString tagName = dtdTag->name();
       TQDomNodeList nodeList = doc.elementsByTagName("tag");
       for ( uint i = 0; i < nodeList.count(); i++)
       {
          TQDomNode n = nodeList.item(i);
          TQString nodeTagName = n.toElement().attribute("name");
          if (!dtdTag->parentDTD->caseSensitive)
              nodeTagName = nodeTagName.upper();
          if (nodeTagName == tagName && n.toElement().elementsByTagName("attr").count() > 0) //read a tag
          {
            mainDlg = new Tagxml( n, dtdTag, m_selection, this );
            ((Tagxml    *)mainDlg)->writeAttributes( dict );
            break;
          }
       }
       }
       f.close();
    }
    else
    {
      TQString docString = "<!DOCTYPE TAGS>\n<TAGS>\n";
      docString += TQString("<tag name=\"%1\">\n").arg(dtdTag->name());
      docString += QuantaCommon::xmlFromAttributes(dtdTag->attributes());
      docString += "</tag>\n</TAGS>\n";
      doc.setContent(docString);
      TQDomNode domNode2 = doc.firstChild().firstChild();
      mainDlg = new Tagxml( domNode2, dtdTag, m_selection, this );
    }

  if ( mainDlg )
  {
    addTab( mainDlg, i18n("Main") );
  }

  TDEConfig *dtdConfig = new TDEConfig(dtdTag->parentDTD->fileName);
  dtdConfig->setGroup("General");
  int numOfPages = dtdConfig->readNumEntry("NumOfPages");
  extraPageList = new TQPtrList<Tagxml>();
  extraPageList->setAutoDelete(true);
  for (int i = 1; i <= numOfPages; i++)
  {
    Tagxml *extraPage = 0L;
    dtdConfig->setGroup(TQString("Page%1").arg(i));
    TQString title = dtdConfig->readEntry("Title");
    TQStringList groupList;
    groupList = dtdConfig->readListEntry("Groups");
    TQDomDocument extraDoc; //build an internal tag XML for the groups
    bool addPage = false;
    TQString docString = "<!DOCTYPE TAGS>\n<TAGS>\n";
    docString += TQString("<tag name=\"Page%1\">\n").arg(i);
    for (uint j = 0; j < groupList.count(); j++)
    {
       groupList[j] = groupList[j].stripWhiteSpace();
    }
    TQDomDocument commonDoc;
    TQString commonFileName = TQFileInfo(dtdTag->fileName()).dirPath() + "/common.tag";
    if (TQFile(commonFileName).exists())
    {
      TQFile commonFile(commonFileName);
      commonFile.open(IO_ReadOnly);
      if (doc.setContent(&commonFile))
      {
        int row = 0;
        TQDomNodeList nodeList = doc.elementsByTagName("tag");
        for ( uint j = 0; j < nodeList.count(); j++)
        {
          TQDomNode node = nodeList.item(j);
          TQString nodeTagName = node.toElement().attribute("name");
          if (groupList.contains(nodeTagName) && dtdTag->commonGroups.contains(nodeTagName)) //add the attributes of this common tag to a new tab
          {
            TQString s;
            TQTextStream str(&s, IO_ReadWrite);
            TQString s2;
            TQTextStream str2(&s2, IO_ReadWrite);
            for ( TQDomNode n = node.firstChild(); !n.isNull(); n = n.nextSibling() )
            {
              if (n.nodeName() == "attr")
              {
                TQDomElement el = n.toElement();
                str.reset();
                str << "<attr name=\"" << el.attribute("name") << "\" type=\"" << el.attribute("type", "input");
                str << "\" source=\"" << el.attribute("source");
                str << "\" interface=\"" << el.attribute("interface");
                str << "\" method=\"" << el.attribute("method");
                str << "\" arguments=\"" << el.attribute("arguments");
                str << "\">" << endl;
                str << "    <text>" << el.attribute("name") << "</text>" << endl;
                str << "    <textlocation col=\"0\" row=\"" << row << "\" />" << endl;
                str << "    <location col=\"1\" row=\"" << row << "\" />" << endl;
                TQDomNodeList childNodes = n.childNodes();
                for ( uint k = 0; k < childNodes.count(); k++)
                {
                  TQDomNode childNode = childNodes.item(j);
                  childNode.save(str2, 2);
                  str << s2;
                }
                str << "</attr>" << endl;
                row++;
              }

            }
            docString += s;
            addPage = true;
          }
        }
        commonFile.close();
      }
    }

    docString += "</tag>\n</TAGS>\n";
    if (addPage)
    {
      extraDoc.setContent(docString);
      TQDomNode domNode = extraDoc.firstChild().firstChild();
      extraPage = new Tagxml( domNode, dtdTag, m_selection, this );
      extraPage->writeAttributes( dict );
      addTab( extraPage, i18n(title.utf8()) );
      extraPageList->append(extraPage);
    }
//    delete attrs;
  }
 }
}

/** Insert an attribute to dict*/
void TagDialog::insertAttribute(TQString *attr, TQString *value)
{
  dict->insert( *attr , value );
}

/** Return the attributes in TQDict<TQString> format*/
TQDict<TQString> * TagDialog::getAttributes()
{
  return dict;
}


/** Return all the attributes in one string*/
TQString TagDialog::getAttributeString()
{
  TQDictIterator<TQString> it( *dict );

  TQString attrStr;

  while ( it.current() ) {
    TQString attr  = QuantaCommon::attrCase(it.currentKey());
    TQString val   = *it.current();

    TQString attrval = " ";  // attr=value

    if ( val.isEmpty()) {
      attrval += attr; // for checkboxes dont print =""
    }
    else {
        attrval += attr + "=" + qConfig.attrValueQuotation + val + qConfig.attrValueQuotation;
    }
    attrStr = attrval + attrStr;

    ++it;
  }
  return attrStr;
}

/**Return the value of the attribute specified by attr. */
TQString TagDialog::getAttribute(const TQString& attr)
{
 TQString attrStr = getAttributeString()+" ";
 int pos = attrStr.upper().find(attr.upper());
 if (pos != -1)
 {
   pos = attrStr.find("=",pos+1) + 1;
   return attrStr.mid(pos, attrStr.find(" ",pos) - pos);
 }
 else
 {
  return TQString();
 }

}

/**  */
void TagDialog::slotAccept()
{

  if (dtdTag->name() != i18n("Unknown tag"))
  {
    if ( mainDlg )
       ((Tagxml *)mainDlg)->readAttributes( dict );
    for (uint i = 0; i < extraPageList->count(); i++)
    {
      extraPageList->at(i)->readAttributes( dict );
    }
    delete extraPageList;
  }
  accept();
}

/** return document path */
KURL TagDialog::baseURL()
{
 return m_baseURL;
}


void TagDialog::parseAttributes(const TQString &attrs)
{
  TQString t = attrs;

  t = t.stripWhiteSpace();

  while ( !t.isEmpty() )
  {
    int i = 0;
    while ( !t[i].isSpace() && !t[i].isNull() && t[i] != '=' )  i++;

    TQString attr = t.left(i);
    TQString *value = new TQString();

    t = t.remove(0,i).stripWhiteSpace();

   if ( t[0] == '=' ) {
      t = t.remove(0,1).stripWhiteSpace();

      if ( t[0] == '"' ) {
        i = 1;
        while ( t[i] != '"' && !t[i].isNull() ) i++;
        if ( t[i] == '"' )
          *value = t.mid(1,i-1);
        else
          *value = t.mid(1,i);
        t = t.remove(0,i+1).stripWhiteSpace();
      }
      else
      if ( t[0] == '\'' ) {
        i = 1;
        while ( t[i] != '\'' && !t[i].isNull() ) i++;
        if ( t[i] == '\'' )
          *value = t.mid(1,i-1);
        else
          *value = t.mid(1,i);
        t = t.remove(0,i+1).stripWhiteSpace();
      }
      else {

        i=0;
        while ( !t[i].isSpace() && !t[i].isNull() )  i++;

        *value = t.left(i);
        t = t.remove(0,i).stripWhiteSpace();
      }

      // debug ( name+" , "+attr[attrcount]+"="+value[attrcount]+";" );




   }
   // tqDebug("attr :%s; value :%s;",attr.data(),value->data() );
   dict->insert( attr , value );
 }

}

/** Insert the new tag into the Document*/
void TagDialog::insertTag(Document *w, bool insertInLine)
{
  if (dtdTag->name() != i18n("Unknown tag"))
  {
    TQString newTag = getAttributeString();
    newTag = TQString("<")+QuantaCommon::tagCase(dtdTag->name())+newTag;

    if ( dtdTag->parentDTD->singleTagStyle == "xml" &&
          (dtdTag->isSingle() || (!qConfig.closeOptionalTags && dtdTag->isOptional()))
        )
    {
      newTag.append(" /");
    }
    newTag.append(">");

    TQString secondPartOfTag = TQString("</")+QuantaCommon::tagCase(dtdTag->name())+">";

    if ( !insertInLine )
    {
      TQString space="";
      space.fill( ' ',w->viewCursorIf->cursorColumnReal() );
      newTag += "\n" + space + "  ";
      secondPartOfTag = "\n" + space + secondPartOfTag;
    }

    if ( (!qConfig.closeTags && !dtdTag->isSingle()) ||
          (dtdTag->isSingle()) ||
          (!qConfig.closeOptionalTags && dtdTag->isOptional()) )
    {
      secondPartOfTag = "";
    }

    w->insertTag( newTag, secondPartOfTag);
  }
}

Node *TagDialog::buildNode(Document *w)
{
	Node *node;
	TQString attrStr("");
	TQDictIterator<TQString> it( *dict );


	node = kafkaCommon::createNode(dtdTag->name(), "", Tag::XmlTag, w);

	while (it.current())
	{
		node->tag->editAttribute(QuantaCommon::attrCase(it.currentKey()), *it.current());
		++it;
	}

	return node;
}

void TagDialog::showEvent(TQShowEvent *ev)
{
  TQTabDialog::showEvent(ev);
  if (dynamic_cast<Tagxml*>(mainDlg))
     static_cast<Tagxml*>(mainDlg)->focusToFirstItem();
}

#include "tagdialog.moc"