Skip navigation.

# Listing 1. KEnvEdit C++ Code ## Listing 1. KEnvEdit C++ Code Note: example code, implementation only. Add your slots with right-clicking on the class item KEnvEdit in KDevelop and select "Add slot". The field "modified" needs to be added as a bool value with selecting "Add variable" in the same manner.
    /***************************************************************************
                              kenvedit.cpp  -  description
                                 -------------------
        begin                : Mon Mai 14 18:14:30 CEST 2001
        copyright            : (C) 2001 by Ralf Nolden
        email                : nolden@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 "kenvedit.h"
    #include "kenvadddlg.h"
    #include
    #include
    #include
    #include
    #include
    #include

    /** the constructor. Brings up the user interface, opens the .bashrc file and inserts the export lines
      * as QListViewItems with variable in column 0 and value in column 1
      */

    KEnvEdit::KEnvEdit(QWidget *parent, const char *name) : KEnvEditDlg(parent, name)
    {
    	modified=false;
        	QFile f(QDir::homeDirPath()+"/.bashrc");
        	if ( f.open(IO_ReadOnly) ) {    // file opened successfully
            	QTextStream t( &f );        // use a text stream
            	QString s;
            	while ( !t.eof() ) {        // until end of file...
    	            	s = t.readLine();       // line of text excluding '\n'
            	    	if (s.contains("export"))
                		{
                    		// remove the export keyword
                    		s.replace( s.find("export"), 6, "" );
                    		// get the left value
                    		QListViewItem * item = new QListViewItem( lv_envar, 0 );
                    		item->setText( 0, s.left( s.find('=' ) ) );
                    		item->setText( 1, s.right( s.length()-(s.find('=')+1) ) );
            		}
    		}
            	f.close();
        	}

    }

    KEnvEdit::~KEnvEdit()
    {
    }
    /** quits the application */
    void KEnvEdit::slotQuit(){
    // TODO: add messagebox to warn the user if the value list is modified. See the modified flag :-)
    //	if(modified)
    	kapp->quit();
    }
    /** Invokes the KDE help system to dislay the manual */
    void KEnvEdit::slotHelp(){
    	kapp->invokeHelp();
    }
    /** calls KEnvAddDlg to add a new variable (kenvadddlg.ui) */
    void KEnvEdit::slotNewVar(){
    	KEnvAddDlg addDlg(this,0,true);
    	if(addDlg.exec() && !(addDlg.le_var->text().isEmpty() || addDlg.le_value->text().isEmpty()) )
    	{
    		QListViewItem * item = new QListViewItem( lv_envar, 0 );
    	    	item->setText( 0, addDlg.le_var->text() );
    	    	item->setText( 1, addDlg.le_value->text() );
    	    	modified=true;
    	}

    }
    /** pops up the KEnvAddDlg, this is the second dialog designed (kenvadddlg.ui) */
    void KEnvEdit::slotEditVar(){
    	KEnvAddDlg addDlg(this,0,true);

       	QListViewItem * item = lv_envar->currentItem();
       	addDlg.le_var->setText( item->text(0) );
       	addDlg.le_value->setText( item->text(1) );

       	if(addDlg.exec() && !(addDlg.le_var->text().isEmpty() || addDlg.le_value->text().isEmpty()) )
       	{
            	item->setText( 0, addDlg.le_var->text() );
            	item->setText( 1, addDlg.le_value->text() );
            	modified=true;
       	}

    }
    /** removes the hash, uncomments the variable, */
    void KEnvEdit::slotEnableVar(){
            QListViewItem* item=lv_envar->currentItem();
            // remove the first letter, which is a # hash to comment out the variable,
    	// so it becomes active again.
            item->setText(0, item->text(0).remove(0,1) );
            slotSelected(item);
            modified=true;
    }
    /** puts a hash in front of the variable. This marks it as commented out,
    	therefore the writing code in slotSave
    	has to put a hash in front of the  export line. */
    void KEnvEdit::slotDisableVar(){
            QListViewItem* item=lv_envar->currentItem();
            item->setText(0, item->text(0).prepend('#') );
            slotSelected(item);
            modified=true;
    }
    /** deletes the variable from the list  */
    void KEnvEdit::slotRemoveVar(){
            lv_envar->takeItem( lv_envar->currentItem() );
            modified=true;

    }
    /** saves the changes to .bashrc  */
    void KEnvEdit::slotSave(){
    	// TODO: Add saving code here
            modified=false;
    }
    /** on a select, enable or disable the according buttons for enabling/ disabling the variable */
    void KEnvEdit::slotSelected(QListViewItem* item){
    	if(item->text(0).contains('#'))
            {
                    pb_enable->setEnabled(true);
                    pb_disable->setEnabled(false);
            }
            else
            {
                    pb_enable->setEnabled(false);
                    pb_disable->setEnabled(true);
            }
    }