1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Word Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-1999 The LyX Team.
9 * This file is Copyright 1996-1999
12 * ======================================================
16 #pragma implementation
22 #include <sys/types.h>
25 #include "bufferlist.h"
27 #include "minibuffer.h"
28 #include "support/FileInfo.h"
29 #include "support/filetools.h"
30 #include "lyx_gui_misc.h"
31 #include "lastfiles.h"
34 #include "lyxscreen.h"
39 #include "vc-backend.h"
41 extern BufferView * current_view;
42 extern int RunLinuxDoc(int, string const &);
49 // Class BufferStorage
52 void BufferStorage::release(Buffer * buf)
54 for(Container::iterator it = container.begin();
55 it != container.end(); ++it) {
57 Buffer * tmpbuf = (*it);
66 Buffer * BufferStorage::newBuffer(string const & s,
70 Buffer * tmpbuf = new Buffer(s, lyxrc, ronly);
71 tmpbuf->params.useClassDefaults();
72 lyxerr.debug() << "Assigning to buffer "
73 << container.size() << endl;
74 container.push_back(tmpbuf);
83 BufferList::BufferList()
84 : state_(BufferList::OK)
88 bool BufferList::empty()
90 return bstore.empty();
94 extern void MenuWrite(Buffer *);
96 bool BufferList::QwriteAll()
98 bool askMoreConfirmation = false;
100 for(BufferStorage::iterator it = bstore.begin();
101 it != bstore.end(); ++it) {
102 if (!(*it)->isLyxClean()) {
103 switch(AskConfirmation(_("Changes in document:"),
104 MakeDisplayPath((*it)->fileName(),
106 _("Save document?"))) {
111 askMoreConfirmation = true;
112 unsaved += MakeDisplayPath((*it)->fileName(),
121 if (askMoreConfirmation &&
122 lyxrc->exit_confirmation &&
123 !AskQuestion(_("Some documents were not saved:"),
124 unsaved, _("Exit anyway?"))) {
132 // Should probably be moved to somewhere else: BufferView? LyXView?
133 bool BufferList::write(Buffer * buf, bool makeBackup)
139 ->Set(_("Saving document"),
140 MakeDisplayPath(buf->fileName()), "...");
142 // We don't need autosaves in the immediate future. (Asger)
143 buf->resetAutosaveTimers();
147 string s = buf->fileName() + '~';
148 // Rename is the wrong way of making a backup,
149 // this is the correct way.
150 /* truss cp fil fil2:
151 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
152 stat("LyXVC.lyx", 0xEFFFF688) = 0
153 open("LyXVC.lyx", O_RDONLY) = 3
154 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
155 fstat(4, 0xEFFFF508) = 0
156 fstat(3, 0xEFFFF508) = 0
157 read(3, " # T h i s f i l e w".., 8192) = 5579
158 write(4, " # T h i s f i l e w".., 5579) = 5579
159 read(3, 0xEFFFD4A0, 8192) = 0
162 chmod("LyXVC3.lyx", 0100644) = 0
163 lseek(0, 0, SEEK_CUR) = 46440
167 // Should proabaly have some more error checking here.
168 // Should be cleaned up in 0.13, at least a bit.
169 // Doing it this way, also makes the inodes stay the same.
170 // This is still not a very good solution, in particular we
171 // might loose the owner of the backup.
172 FileInfo finfo(buf->fileName());
174 mode_t fmode = finfo.getMode();
175 struct utimbuf * times = new struct utimbuf;
177 times->actime = finfo.getAccessTime();
178 times->modtime = finfo.getModificationTime();
179 ifstream ifs(buf->fileName().c_str());
180 ofstream ofs(s.c_str(), ios::out|ios::trunc);
192 chmod(s.c_str(), fmode);
194 if (utime(s.c_str(), times)) {
195 lyxerr << "utime error." << endl;
198 lyxerr << "LyX was not able to make "
199 "backupcopy. Beware." << endl;
205 if (buf->writeFile(buf->fileName(), false)) {
208 current_view->owner()->getMiniBuffer()->
209 Set(_("Document saved as"),
210 MakeDisplayPath(buf->fileName()));
212 // now delete the autosavefile
213 string a = OnlyPath(buf->fileName());
215 a += OnlyFilename(buf->fileName());
217 FileInfo fileinfo(a);
218 if (fileinfo.exist()) {
219 if (remove(a.c_str()) != 0) {
220 WriteFSAlert(_("Could not delete "
221 "auto-save file!"), a);
225 // Saving failed, so backup is not backup
227 string s = buf->fileName() + '~';
228 rename(s.c_str(), buf->fileName().c_str());
230 current_view->owner()->getMiniBuffer()->Set(_("Save failed!"));
238 void BufferList::closeAll()
240 state_ = BufferList::CLOSING;
241 while (!bstore.empty()) {
242 close(bstore.front());
244 state_ = BufferList::OK;
248 void BufferList::resize()
250 for(BufferStorage::iterator it = bstore.begin();
251 it != bstore.end(); ++it) {
257 bool BufferList::close(Buffer * buf)
259 if (buf->getUser()) buf->getUser()->insetUnlock();
261 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
263 switch(AskConfirmation(_("Changes in document:"),
264 MakeDisplayPath(buf->fileName(), 50),
265 _("Save document?"))){
267 if (write(buf, lyxrc->make_backup)) {
268 lastfiles->newFile(buf->fileName());
286 vector<string> BufferList::getFileNames() const
289 for(BufferStorage::const_iterator cit = bstore.begin();
290 cit != bstore.end(); ++cit) {
291 nvec.push_back((*cit)->fileName());
297 Buffer * BufferList::first()
299 if (bstore.empty()) return 0;
300 return bstore.front();
304 Buffer * BufferList::getBuffer(int choice)
306 if (choice >= bstore.size()) return 0;
307 return bstore[choice];
311 void BufferList::updateInset(Inset * inset, bool mark_dirty)
313 for (BufferStorage::iterator it = bstore.begin();
314 it != bstore.end(); ++it) {
316 && (*it)->getUser()->text->UpdateInset(inset)) {
325 int BufferList::unlockInset(UpdatableInset * inset)
327 if (!inset) return 1;
328 for(BufferStorage::iterator it = bstore.begin();
329 it != bstore.end(); ++it) {
331 && (*it)->getUser()->the_locking_inset == inset) {
332 (*it)->getUser()->insetUnlock();
340 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
342 for(BufferStorage::iterator it = bstore.begin();
343 it != bstore.end(); ++it) {
344 if (!(*it)->isDepClean(mastertmpdir)) {
345 string writefile = mastertmpdir;
347 writefile += ChangeExtension((*it)->fileName(),
349 (*it)->makeLaTeXFile(writefile, mastertmpdir,
351 (*it)->markDepClean(mastertmpdir);
357 void BufferList::emergencyWriteAll()
359 for (BufferStorage::iterator it = bstore.begin();
360 it != bstore.end(); ++it) {
361 if (!(*it)->isLyxClean()) {
364 lyxerr <<_("lyx: Attempting to save"
367 << _(" as...") << endl;
369 for (int i = 0; i < 3 && !madeit; ++i) {
372 // We try to save three places:
373 // 1) Same place as document.
374 // 2) In HOME directory.
375 // 3) In "/tmp" directory.
377 s = (*it)->fileName();
379 s = AddName(GetEnvPath("HOME"),
382 // MakeAbsPath to prepend the current
383 // drive letter on OS/2
384 s = AddName(MakeAbsPath("/tmp/"),
389 lyxerr << " " << i + 1 << ") " << s << endl;
391 if ((*it)->writeFile(s, true)) {
392 (*it)->markLyxClean();
393 lyxerr << _(" Save seems successful. "
397 lyxerr << _(" Save failed! Trying...")
400 lyxerr << _(" Save failed! Bummer. "
410 Buffer * BufferList::readFile(string const & s, bool ronly)
412 Buffer * b = bstore.newBuffer(s, lyxrc, ronly);
415 string e = OnlyPath(s);
417 // File information about normal file
418 FileInfo fileInfo2(s);
420 // Check if emergency save file exists and is newer.
421 e += OnlyFilename(s) + ".emergency";
422 FileInfo fileInfoE(e);
424 bool use_emergency = false;
426 if (fileInfoE.exist() && fileInfo2.exist()) {
427 if (fileInfoE.getModificationTime()
428 > fileInfo2.getModificationTime()) {
429 if (AskQuestion(_("An emergency save of this document exists!"),
430 MakeDisplayPath(s, 50),
431 _("Try to load that instead?"))) {
433 // the file is not saved if we load the
436 use_emergency = true;
438 // Here, we should delete the emergency save
444 if (!use_emergency) {
445 // Now check if autosave file is newer.
447 a += OnlyFilename(s);
449 FileInfo fileInfoA(a);
450 if (fileInfoA.exist() && fileInfo2.exist()) {
451 if (fileInfoA.getModificationTime()
452 > fileInfo2.getModificationTime()) {
453 if (AskQuestion(_("Autosave file is newer."),
454 MakeDisplayPath(s, 50),
455 _("Load that one instead?"))) {
457 // the file is not saved if we load the
461 // Here, we should delete the autosave
467 // not sure if this is the correct place to begin LyXLex
470 if (b->readFile(lex))
479 bool BufferList::exists(string const & s)
481 for (BufferStorage::iterator it = bstore.begin();
482 it != bstore.end(); ++it) {
483 if ((*it)->fileName() == s)
490 Buffer * BufferList::getBuffer(string const & s)
492 for(BufferStorage::iterator it = bstore.begin();
493 it != bstore.end(); ++it) {
494 if ((*it)->fileName() == s)
501 Buffer * BufferList::newFile(string const & name, string tname)
503 /* get a free buffer */
504 Buffer * b = bstore.newBuffer(name, lyxrc);
506 // use defaults.lyx as a default template if it exists.
508 tname = LibFileSearch("templates", "defaults.lyx");
510 if (!tname.empty() && IsLyXFilename(tname)){
511 bool templateok = false;
515 if (b->readFile(lex)) {
520 WriteAlert(_("Error!"), _("Unable to open template"),
521 MakeDisplayPath(tname));
522 // no template, start with empty buffer
523 b->paragraph = new LyXParagraph;
526 else { // start with empty buffer
527 b->paragraph = new LyXParagraph;
531 b->setReadonly(false);
537 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
539 // make sure our path is absolute
540 string s = MakeAbsPath(filename);
542 // Is this done too early?
544 if (IsSGMLFilename(s)) {
546 if (fi.exist() && fi.readable()) {
547 if (!RunLinuxDoc(-1, s)) {
548 s = ChangeExtension (s, ".lyx", false);
549 } else { // sgml2lyx failed
550 WriteAlert(_("Error!"),
551 _("Could not convert file"), s);
555 // just change the extension and it will be
556 // handled like a regular lyx file that does
558 s = ChangeExtension(s, ".lyx", false);
562 // file already open?
564 if (AskQuestion(_("Document is already open:"),
565 MakeDisplayPath(s, 50),
566 _("Do you want to reload that document?"))) {
567 // Reload is accomplished by closing and then loading
568 if (!close(getBuffer(s))) {
571 // Fall through to new load. (Asger)
573 // Here, we pretend that we just loaded the
580 switch (IsFileWriteable(s)) {
582 current_view->owner()->getMiniBuffer()->
583 Set(_("File `")+MakeDisplayPath(s, 50)+
584 _("' is read-only."));
590 b->lyxvc.file_found_hook(s);
592 break; //fine- it's r/w
594 // Here we probably should run
595 if (LyXVC::file_not_found_hook(s)) {
596 // Ask if the file should be checked out for
597 // viewing/editing, if so: load it.
598 if (AskQuestion(_("Do you want to retrive file under version control?"))) {
599 // How can we know _how_ to do the checkout?
600 // With the current VC support it has to be,
601 // a RCS file since CVS do not have special ,v files.
603 return loadLyXFile(filename, tolastfiles);
606 if (AskQuestion(_("Cannot open specified file:"),
607 MakeDisplayPath(s, 50),
608 _("Create new document with this name?")))
610 // Find a free buffer
611 b = newFile(s, string());
616 if (b && tolastfiles)
617 lastfiles->newFile(b->fileName());