# HG changeset patch
# Parent 22ca875e71f452099ba5b264fea3732ac37493fa
# User Israel Herraiz <israel.herraiz@upm.es>
Sync directories between GUI and Octave. Fixes #35618

diff -r 22ca875e71f4 -r 9355a11750d1 gui/src/FilesDockWidget.cpp
--- a/gui/src/FilesDockWidget.cpp	Sun Apr 22 17:42:40 2012 -0700
+++ b/gui/src/FilesDockWidget.cpp	Tue Apr 24 19:08:30 2012 -0700
@@ -78,10 +78,14 @@
 
   setCurrentDirectory (m_fileSystemModel->fileInfo (rootPathIndex).
 		       absoluteFilePath ());
+  emit changeWorkingDirectory(m_fileSystemModel->fileInfo (rootPathIndex).
+		       absoluteFilePath());
 
   connect (m_fileTreeView, SIGNAL (doubleClicked (const QModelIndex &)), this,
 	   SLOT (itemDoubleClicked (const QModelIndex &)));
-
+  connect (OctaveLink::instance(), SIGNAL (workingDirUpdated (QString)), this,
+           SLOT (onDirChangeFromOctave (QString)));
+  
   // Layout the widgets vertically with the toolbar on top
   QVBoxLayout *
     layout = new QVBoxLayout ();
@@ -114,6 +118,8 @@
       m_fileTreeView->setRootIndex (index);
       setCurrentDirectory (m_fileSystemModel->fileInfo (index).
 			   absoluteFilePath ());
+      emit changeWorkingDirectory(m_fileSystemModel->fileInfo (index).
+				  absoluteFilePath());
     }
   // Otherwise attempt to open it.
   else
@@ -140,6 +146,19 @@
   m_currentDirectory->setText (currentDirectory);
 }
 
+void 
+FilesDockWidget::onDirChangeFromOctave (QString dirName)
+{
+  if (m_currentDirectory->text() != dirName)
+    {
+      m_fileSystemModel->setRootPath (dirName);
+      m_fileTreeView->setRootIndex (m_fileSystemModel->index 
+				    (dirName));
+      setCurrentDirectory (dirName);
+    }
+}
+
+
 void
 FilesDockWidget::onUpDirectory (void)
 {
@@ -150,6 +169,7 @@
   m_fileTreeView->setRootIndex (m_fileSystemModel->
 				index (dir.absolutePath ()));
   setCurrentDirectory (dir.absolutePath ());
+  emit changeWorkingDirectory (dir.absolutePath ());
 }
 
 void
@@ -162,6 +182,7 @@
 				    index (fileInfo.absolutePath ()));
       m_fileSystemModel->setRootPath (fileInfo.absolutePath ());
       setCurrentDirectory (fileInfo.absoluteFilePath ());
+      emit changeWorkingDirectory (fileInfo.absoluteFilePath ());
     }
   else
     {
diff -r 22ca875e71f4 -r 9355a11750d1 gui/src/FilesDockWidget.h
--- a/gui/src/FilesDockWidget.h	Sun Apr 22 17:42:40 2012 -0700
+++ b/gui/src/FilesDockWidget.h	Tue Apr 24 19:08:30 2012 -0700
@@ -33,6 +33,8 @@
 #include <QDockWidget>
 #include <QLineEdit>
 
+#include "OctaveLink.h"
+
 class FilesDockWidget:public QDockWidget
 {
   Q_OBJECT
@@ -47,6 +49,7 @@
   void onUpDirectory ();
 
   void setCurrentDirectory (QString currentDirectory);
+  void onDirChangeFromOctave (QString dirName);
 
   void currentDirectoryEntered ();
 
@@ -59,6 +62,8 @@
 
   /** Custom signal that tells if a user has clicke away that dock widget. */
   void activeChanged (bool active);
+  
+  void changeWorkingDirectory (QString dirName);
 
 protected:
   void closeEvent (QCloseEvent *event);
diff -r 22ca875e71f4 -r 9355a11750d1 gui/src/MainWindow.cpp
--- a/gui/src/MainWindow.cpp	Sun Apr 22 17:42:40 2012 -0700
+++ b/gui/src/MainWindow.cpp	Tue Apr 24 19:08:30 2012 -0700
@@ -233,6 +233,12 @@
   QMessageBox::aboutQt (this);
 }
 
+void 
+MainWindow::setCWDInOCtave (QString dirName)
+{
+  m_terminalView->sendText (QString("cd \'%1\'\n").arg (dirName));
+}
+
 void
 MainWindow::closeEvent (QCloseEvent * closeEvent)
 {
@@ -376,6 +382,7 @@
   connect (this, SIGNAL (settingsChanged ()), m_filesDockWidget, SLOT (noticeSettings ()));
 
   connect (m_filesDockWidget, SIGNAL (openFile (QString)), this, SLOT (handleOpenFileRequest (QString)));
+  connect (m_filesDockWidget, SIGNAL (changeWorkingDirectory (QString)), this, SLOT (setCWDInOCtave (QString)));
   connect (m_historyDockWidget, SIGNAL (information (QString)), this, SLOT (reportStatusMessage (QString)));
   connect (m_historyDockWidget, SIGNAL (commandDoubleClicked (QString)), this, SLOT (handleCommandDoubleClicked (QString)));
   connect (saveWorkspaceAction, SIGNAL (triggered ()), this, SLOT (handleSaveWorkspaceRequest ()));
diff -r 22ca875e71f4 -r 9355a11750d1 gui/src/MainWindow.h
--- a/gui/src/MainWindow.h	Sun Apr 22 17:42:40 2012 -0700
+++ b/gui/src/MainWindow.h	Tue Apr 24 19:08:30 2012 -0700
@@ -102,6 +102,7 @@
   void processSettingsDialogRequest ();
   void showAboutOctave ();
   void showAboutQt ();
+  void setCWDInOCtave (QString dirName);
 
 protected:
   void closeEvent (QCloseEvent * closeEvent);
diff -r 22ca875e71f4 -r 9355a11750d1 gui/src/backend/OctaveCallbackThread.cpp
--- a/gui/src/backend/OctaveCallbackThread.cpp	Sun Apr 22 17:42:40 2012 -0700
+++ b/gui/src/backend/OctaveCallbackThread.cpp	Tue Apr 24 19:08:30 2012 -0700
@@ -40,6 +40,7 @@
   while (running)
     {
       OctaveLink::instance ()->emitSymbolTableChanged();
+      OctaveLink::instance ()->emitWorkingDirUpdated ();
       OctaveLink::instance ()->updateHistoryModel ();
       usleep (500000);
 
diff -r 22ca875e71f4 -r 9355a11750d1 gui/src/backend/OctaveLink.h
--- a/gui/src/backend/OctaveLink.h	Sun Apr 22 17:42:40 2012 -0700
+++ b/gui/src/backend/OctaveLink.h	Tue Apr 24 19:08:30 2012 -0700
@@ -53,6 +53,7 @@
 #include "octave/unwind-prot.h"
 #include "octave/utils.h"
 #include "octave/variables.h"
+#include "octave/oct-env.h"
 
 // Standard includes
 #include <iostream>
@@ -114,9 +115,11 @@
   void updateHistoryModel ();
   QStringListModel *historyModel ();
   void emitSymbolTableChanged() { emit symbolTableChanged(); }
+  void emitWorkingDirUpdated () { emit workingDirUpdated (QString::fromStdString(octave_env::get_current_directory()));}
 
 signals:
   void symbolTableChanged ();
+  void workingDirUpdated (QString);
 
 private:
   OctaveLink ();
