00001 /******************************************************************************* 00002 * * 00003 * Copyright (C) 2003 Erik Sjolund, (<html>Erik Sjölund</html>) * 00004 * Center for Genomics and Bioinformatics, * 00005 * Karolinska Institutet, * 00006 * Stockholm, * 00007 * Sweden * 00008 * * 00009 * Author: Erik Sjolund * 00010 * Email: erik.sjolund@adivo.se * 00011 * * 00012 ******************************************************************************* 00013 */ 00014 #include "trapperconf.h" 00015 #include <qmessagebox.h> 00016 #include <qfile.h> 00017 #include <cstdlib> 00018 00019 TrapperConf * TrapperConf::s_instance = NULL; 00020 00021 TrapperConf::TrapperConf() 00022 : QObject() 00023 { 00024 00025 00026 /* 00027 QString name = fileName(); 00028 QFile confFile( name ); 00029 if ( !confFile.open( IO_ReadWrite ) ) 00030 { 00031 QMessageBox::critical( 0, 00032 tr( "Critical Error" ), 00033 tr( "Cannot open configuration file %1" ).arg( fileName() ) ); 00034 exit(1); 00035 } 00036 */ 00037 00038 } 00039 00040 void TrapperConf::loadDom(QFile * file) 00041 { 00042 00043 00044 confFile = file; 00045 00046 QString errorMsg; 00047 int errorLine; 00048 int errorColumn; 00049 Q_CHECK_PTR( confFile ); 00050 if ( !domTree.setContent( confFile , &errorMsg, &errorLine, &errorColumn) ) 00051 { 00052 QMessageBox::critical( 0, 00053 tr( "Critical Error" ), 00054 tr( "Parsing error for configuration file %1 Error in line=%2 column=%3 error message=\"%4\"").arg( confFile->name() ).arg(errorLine).arg(errorColumn).arg(errorMsg)); 00055 exit(1); 00056 } 00057 return; 00058 } 00059 00060 void TrapperConf::save() 00061 { 00062 00063 Q_CHECK_PTR( confFile ); 00064 /* 00065 QFile confFile( fileName() ); 00066 if ( !confFile.open( IO_WriteOnly ) ) 00067 { 00068 QMessageBox::critical( 0, 00069 tr( "Critical Error" ), 00070 tr( "Cannot open configuration file %1" ).arg( fileName() ) ); 00071 exit(1); 00072 } 00073 */ 00074 QTextStream stream( confFile ); 00075 QString xml = domTree.toString(); 00076 stream << xml; 00077 } 00078 00079 TrapperConf::~TrapperConf() 00080 {} 00081 00082 QDomElement TrapperConf::viewModeConf() 00083 { 00084 // get the configuration information from the DOM 00085 QDomElement root = domTree.documentElement(); 00086 QDomNode node; 00087 QDomElement element; 00088 node = root.firstChild(); 00089 bool found = false; 00090 while ( !node.isNull() ) 00091 { 00092 if ( node.isElement() && node.nodeName() == "modeViews" ) 00093 { 00094 element = node.toElement(); 00095 found = true; 00096 break; 00097 } 00098 node = node.nextSibling(); 00099 } 00100 if ( found == false ) 00101 { 00102 QMessageBox::critical( 0, tr("Critical Error"), tr("Conf file has bad syntax")); 00103 exit(1); 00104 } 00105 return element; 00106 } 00107 00108 QStringList TrapperConf::viewModeNames() 00109 { 00110 00111 QDomElement confElem = viewModeConf(); 00112 00113 QDomNode node = confElem.firstChild(); 00114 00115 QStringList list; 00116 while ( !node.isNull() ) 00117 { 00118 if ( node.isElement() && node.nodeName() == "modeView" ) 00119 { 00120 QDomElement elem = node.toElement(); 00121 Q_ASSERT((elem.hasAttribute("name") && elem.hasAttribute("zoom"))); 00122 QString name = elem.attribute("name", ""); 00123 list.append(name); 00124 } 00125 node = node.nextSibling(); 00126 } 00127 return list; 00128 } 00129 00130 TrapperConf * TrapperConf::instance() 00131 { 00132 if ( s_instance == NULL ) 00133 { 00134 s_instance = new TrapperConf; 00135 } 00136 return s_instance; 00137 }