home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / lyx-0.13.2.tar.gz / lyx-0.13.2.tar / lyx-0.13.2 / src / lyxvc.C < prev    next >
C/C++ Source or Header  |  1998-04-23  |  7KB  |  321 lines

  1. #include <config.h>
  2.  
  3. #ifdef __GNUG__
  4. #pragma implementation
  5. #endif
  6.  
  7. #include FORMS_H_LOCATION
  8. #include "lyxvc.h"
  9. #include "error.h"
  10. #include "lyx_gui_misc.h"
  11. #include "bufferlist.h"
  12. #include "syscall.h"
  13. #include "path.h"
  14. #include "filetools.h"
  15. #include "FileInfo.h"
  16. #include "gettext.h"
  17. #include "LyXView.h"
  18. #include "lyxfunc.h"
  19. #include "latexoptions.h"
  20.  
  21. extern BufferList bufferlist;
  22.  
  23. LyXVC::LyXVC()
  24. {
  25.     backend = UNKNOWN_VCS;
  26.     _owner = 0;
  27.     browser = 0;
  28. }
  29.  
  30.  
  31. LyXVC::~LyXVC()
  32. {
  33.     if (browser) {
  34.         if (browser->LaTeXLog->visible)
  35.             fl_hide_form(browser->LaTeXLog);
  36.         fl_free_form(browser->LaTeXLog);
  37.     }
  38. }
  39.  
  40.  
  41. bool LyXVC::file_found_hook(LString const & fn)
  42. {
  43.     LString tmp(fn);
  44.     FileInfo f;
  45.     // Check if *,v exists.
  46.     tmp += ",v";
  47.     lyxerr.debug(LString("Checking if file is under vc: ") + tmp, Error::LYXVC);
  48.     if (f.newFile(tmp).readable()) {
  49.         lyxerr.debug("Yes it is under vc.", Error::LYXVC);
  50.         master = tmp;
  51.         backend = RCS_VCS;
  52.         scanMaster();
  53.         return true;
  54.     } else {
  55.         // Check if RCS/*,v exists.
  56.         tmp = AddName(AddPath(OnlyPath(fn), "RCS"), fn);
  57.         tmp += ",v";
  58.         lyxerr.debug("Checking if file is under vc: " + tmp, Error::LYXVC);
  59.         if (f.newFile(tmp).readable()) {
  60.             lyxerr.debug("Yes it is under vc.", Error::LYXVC);
  61.             master = tmp;
  62.             backend = RCS_VCS;
  63.             scanMaster();
  64.             return true;
  65.         }
  66.     }
  67.     // If either one, return true
  68.  
  69.     // file is not under any VCS.
  70.     return false;
  71. }
  72.  
  73.  
  74. bool LyXVC::file_not_found_hook(LString const &)
  75. {
  76.     // file is not under any VCS.
  77.     return false;
  78. }
  79.  
  80.  
  81. void LyXVC::scanMaster()
  82. {
  83.     lyxerr.debug("LyXVC: This file is a VC file.", Error::LYXVC);
  84.  
  85.     LyXLex lex(NULL, 0);
  86.     lex.setFile(master);
  87.  
  88.     LString token;
  89.     bool read_enough = false;
  90.     while (lex.IsOK() && !read_enough) {
  91.         lex.next();
  92.         token = lex.GetString();
  93.         
  94.         lyxerr.debug("LyXVC::scanMaster: current lex text: `"
  95.                 +token+"'", Error::LYXVC);
  96.  
  97.         if (token.empty())
  98.             continue;
  99.         else if (token == "head") {
  100.             // get version here
  101.             lex.next();
  102.             LString tmv = lex.GetString();
  103.             tmv.strip(';');
  104.             version = tmv;
  105.         } else if (token.contains("access")
  106.                || token.contains("symbols")
  107.                || token.contains("strict")) {
  108.             // nothing
  109.         } else if (token.contains("locks")) {
  110.             // get locker here
  111.             if (token.contains(";")) {
  112.                 locker = "Unlocked";
  113.                 vcstat = UNLOCKED;
  114.                 continue;
  115.             }
  116.             LString tmpt, s1, s2;
  117.             do {
  118.                 lex.next();
  119.                 tmpt = lex.GetString();
  120.                 s1 = tmpt;
  121.                 s1.strip(';');
  122.                 // tmp is now in the format <user>:<version>
  123.                 s1.split(s2, ':');
  124.                 // s2 is user, and s1 is version
  125.                 if (s1 == version) {
  126.                     locker = s2;
  127.                     vcstat = LOCKED;
  128.                     break;
  129.                 }
  130.             } while (!tmpt.contains(";"));
  131.             
  132.         } else if (token == "comment") {
  133.             // we don't need to read any further than this.
  134.             read_enough = true;
  135.         } else {
  136.             // unexpected
  137.             lyxerr.debug("LyXVC::scanMaster(): unexpected token", Error::LYXVC);
  138.         }
  139.     }
  140. }
  141.  
  142.  
  143. void LyXVC::setBuffer(Buffer *buf)
  144. {
  145.     _owner = buf;
  146. }
  147.  
  148.  
  149. //
  150. // I will probably add some backend_xxxx functions later to perform the
  151. // version control system specific commands. Something like:
  152. // void backend_revert(<params>) {
  153. //        if (backend == "RCS") {
  154. //        } else if (backend == "CVS") {
  155. //        } else if (backend == "SCCS") {
  156. //        }
  157. //
  158. // But for 0.12 we will only support RCS.
  159. //
  160.  
  161. void LyXVC::registrer()
  162. {
  163.     lyxerr.debug("LyXVC: registrer", Error::LYXVC);
  164.     LString tmp = askForText(_("LyX VC: Initial description"));
  165.     LString cmd = "ci -q -u -i -t-\"";
  166.     cmd += tmp;
  167.     cmd += "\" ";
  168.     cmd += OnlyFilename(_owner->getFileName());
  169.     doVCCommand(cmd);
  170.     _owner->getUser()->getOwner()->getLyXFunc()->Dispatch("buffer-reload");
  171. }
  172.  
  173.  
  174. void LyXVC::checkIn()
  175. {
  176.     lyxerr.debug("LyXVC: checkIn", Error::LYXVC);
  177.     _owner->getUser()->getOwner()->getLyXFunc()->Dispatch(LFUN_MENUWRITE);
  178.     LString tmp = askForText(_("LyX VC: Log Message"));
  179.     doVCCommand("ci -q -u -m\"" + tmp + "\" "
  180.             + OnlyFilename(_owner->getFileName()));
  181.     _owner->getUser()->getOwner()->getLyXFunc()->Dispatch("buffer-reload");
  182. }
  183.  
  184.  
  185. void LyXVC::checkOut()
  186. {
  187.     lyxerr.debug("LyXVC: checkOut", Error::LYXVC);
  188.     doVCCommand("co -q -l "
  189.             + OnlyFilename(_owner->getFileName()));
  190.     _owner->getUser()->getOwner()->getLyXFunc()->Dispatch("buffer-reload");
  191. }
  192.  
  193.  
  194. void LyXVC::revert()
  195. {
  196.     lyxerr.debug("LyXVC: revert", Error::LYXVC);
  197.     // Here we should check if the buffer is dirty. And if it is
  198.     // we should warn the user that reverting will discard all
  199.     // changes made since the last check in.
  200.     if (AskQuestion("LyX VC:",
  201.             _("When reverting you will lose all changes made"
  202.             " to the document since the last check-in."),
  203.             _("Do you still want to do it?"))) {
  204.         
  205.         doVCCommand("co -f -u" + getVersion() + ' '
  206.                 + OnlyFilename(_owner->getFileName()));
  207.         _owner->getUser()->getOwner()->
  208.             getLyXFunc()->Dispatch("buffer-reload");
  209.     }
  210. }
  211.  
  212.  
  213. void LyXVC::undoLast()
  214. {
  215.     lyxerr.debug("LyXVC: undoLast", Error::LYXVC);
  216.     doVCCommand("rcs -o" + getVersion() + ' '
  217.             + OnlyFilename(_owner->getFileName()));
  218. }
  219.  
  220.  
  221. void LyXVC::toggleReadOnly()
  222. {
  223.     switch (vcstat) {
  224.     case UNLOCKED:
  225.         checkOut();
  226.         break;
  227.     case LOCKED:
  228.         checkIn();
  229.         break;
  230.     }
  231. }
  232.  
  233.  
  234. bool LyXVC::inUse()
  235. {
  236.     if (!master.empty())
  237.         return true;
  238.     return false;
  239. }
  240.  
  241.  
  242. LString const LyXVC::getVersion() const
  243. {
  244.     return version;
  245. }
  246.  
  247.  
  248. LString const LyXVC::getLocker() const
  249. {
  250.     return locker;
  251. }
  252.  
  253. // This is a hack anyway so I'll put it here in the mean time.
  254. void LyXVC::logClose(FL_OBJECT *obj, long)
  255. {
  256.     LyXVC *This = (LyXVC*)obj->form->u_vdata;
  257.     fl_hide_form(This->browser->LaTeXLog);
  258. }
  259.  
  260.  
  261. void LyXVC::logUpdate(FL_OBJECT *obj, long)
  262. {
  263.     LyXVC *This = (LyXVC*)obj->form->u_vdata;
  264.     This->showLog();
  265. }
  266.  
  267.  
  268. void LyXVC::viewLog(LString const & fil)
  269. {
  270.     if (!browser) {
  271.         FL_OBJECT *obj;
  272.         browser = (FD_LaTeXLog *) fl_calloc(1, sizeof(*browser));
  273.         
  274.         browser->LaTeXLog = fl_bgn_form(FL_NO_BOX, 470, 380);
  275.         browser->LaTeXLog->u_vdata = (void*)this;
  276.         obj = fl_add_box(FL_UP_BOX, 0, 0, 470, 380, "");
  277.         browser->browser_latexlog = fl_add_browser(FL_NORMAL_BROWSER, 10, 10, 450, 320, "");
  278.         obj = fl_add_button(FL_RETURN_BUTTON, 270, 340, 90, 30, _("Close"));
  279.         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
  280.         fl_set_object_callback(obj, logClose, 0);
  281.         obj = fl_add_button(FL_NORMAL_BUTTON,370,340,90,30,
  282.                     idex(_("Update|#Uu")));
  283.         fl_set_button_shortcut(obj,scex(_("Update|#Uu")),1);
  284.         fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  285.         fl_set_object_callback(obj,logUpdate,0);
  286.         fl_end_form();
  287.         fl_set_form_atclose(browser->LaTeXLog, CancelCloseBoxCB, NULL);
  288.     }
  289.  
  290.     if (!fl_load_browser(browser->browser_latexlog, fil.c_str()))
  291.         fl_add_browser_line(browser->browser_latexlog, _("No RCS History!"));
  292.     
  293.     if (browser->LaTeXLog->visible) {
  294.         fl_raise_form(browser->LaTeXLog);
  295.     } else {
  296.         fl_show_form(browser->LaTeXLog,
  297.                  FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
  298.                  _("RCS History"));
  299.     }
  300. }
  301.  
  302.  
  303. void LyXVC::showLog()
  304. {
  305.     LString tmpf = tmpnam(NULL);
  306.     doVCCommand("rlog "
  307.             + OnlyFilename(_owner->getFileName()) + " > " + tmpf);
  308.     viewLog(tmpf);
  309.     unlink(tmpf.c_str());
  310. }
  311.  
  312.  
  313. int LyXVC::doVCCommand(LString const & cmd)
  314. {
  315.     lyxerr.debug("doVCCommand: " + cmd, Error::LYXVC);
  316.     Path p(_owner->filepath);
  317.         Systemcalls one;
  318.     int ret = one.Startscript(Systemcalls::System, cmd);
  319.     return ret;
  320. }
  321.