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() + 1 << endl;
74 container.push_back(tmpbuf);
83 BufferList::BufferList()
85 _state = BufferList::OK;
89 bool BufferList::empty()
91 return bstore.empty();
95 extern void MenuWrite(Buffer *);
97 bool BufferList::QwriteAll()
99 bool askMoreConfirmation = false;
101 for(BufferStorage::iterator it = bstore.begin();
102 it != bstore.end(); ++it) {
103 if (!(*it)->isLyxClean()) {
104 switch(AskConfirmation(_("Changes in document:"),
105 MakeDisplayPath((*it)->fileName(),
107 _("Save document?"))) {
112 askMoreConfirmation = true;
113 unsaved += MakeDisplayPath((*it)->fileName(),
122 if (askMoreConfirmation &&
123 lyxrc->exit_confirmation &&
124 !AskQuestion(_("Some documents were not saved:"),
125 unsaved, _("Exit anyway?"))) {
133 // Should probably be moved to somewhere else: BufferView? LyXView?
134 bool BufferList::write(Buffer * buf, bool makeBackup)
140 ->Set(_("Saving document"),
141 MakeDisplayPath(buf->fileName()), "...");
143 // We don't need autosaves in the immediate future. (Asger)
144 buf->resetAutosaveTimers();
148 string s = buf->fileName() + '~';
149 // Rename is the wrong way of making a backup,
150 // this is the correct way.
151 /* truss cp fil fil2:
152 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
153 stat("LyXVC.lyx", 0xEFFFF688) = 0
154 open("LyXVC.lyx", O_RDONLY) = 3
155 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
156 fstat(4, 0xEFFFF508) = 0
157 fstat(3, 0xEFFFF508) = 0
158 read(3, " # T h i s f i l e w".., 8192) = 5579
159 write(4, " # T h i s f i l e w".., 5579) = 5579
160 read(3, 0xEFFFD4A0, 8192) = 0
163 chmod("LyXVC3.lyx", 0100644) = 0
164 lseek(0, 0, SEEK_CUR) = 46440
168 // Should proabaly have some more error checking here.
169 // Should be cleaned up in 0.13, at least a bit.
170 // Doing it this way, also makes the inodes stay the same.
171 // This is still not a very good solution, in particular we
172 // might loose the owner of the backup.
173 FileInfo finfo(buf->fileName());
175 mode_t fmode = finfo.getMode();
176 struct utimbuf * times = new struct utimbuf;
178 times->actime = finfo.getAccessTime();
179 times->modtime = finfo.getModificationTime();
180 ifstream ifs(buf->fileName().c_str());
181 ofstream ofs(s.c_str(), ios::out|ios::trunc);
189 chmod(s.c_str(), fmode);
191 if (utime(s.c_str(), times)) {
192 lyxerr << "utime error." << endl;
195 lyxerr << "LyX was not able to make "
196 "backupcopy. Beware." << endl;
202 if (buf->writeFile(buf->fileName(), false)) {
205 current_view->owner()->getMiniBuffer()->
206 Set(_("Document saved as"),
207 MakeDisplayPath(buf->fileName()));
209 // now delete the autosavefile
210 string a = OnlyPath(buf->fileName());
212 a += OnlyFilename(buf->fileName());
214 FileInfo fileinfo(a);
215 if (fileinfo.exist()) {
216 if (remove(a.c_str()) != 0) {
217 WriteFSAlert(_("Could not delete "
218 "auto-save file!"), a);
222 // Saving failed, so backup is not backup
224 string s = buf->fileName() + '~';
225 rename(s.c_str(), buf->fileName().c_str());
227 current_view->owner()->getMiniBuffer()->Set(_("Save failed!"));
235 void BufferList::closeAll()
237 _state = BufferList::CLOSING;
238 while (!bstore.empty()) {
239 close(bstore.front());
241 _state = BufferList::OK;
245 void BufferList::resize()
247 for(BufferStorage::iterator it = bstore.begin();
248 it != bstore.end(); ++it) {
254 bool BufferList::close(Buffer * buf)
256 if (buf->getUser()) buf->getUser()->insetUnlock();
258 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
260 switch(AskConfirmation(_("Changes in document:"),
261 MakeDisplayPath(buf->fileName(), 50),
262 _("Save document?"))){
264 if (write(buf, lyxrc->make_backup)) {
265 lastfiles->newFile(buf->fileName());
283 void BufferList::makePup(int pup)
284 /* This should be changed to return a char const *const
285 in the same way as for lastfiles.[hC]
289 for(BufferStorage::iterator it = bstore.begin();
290 it != bstore.end(); ++it) {
291 string relbuf = MakeDisplayPath((*it)->fileName(), 30);
292 fl_addtopup(pup, relbuf.c_str());
295 if (ant == 0) fl_addtopup(pup, _("No Documents Open!%t"));
299 Buffer * BufferList::first()
301 if (bstore.empty()) return 0;
302 return bstore.front();
306 Buffer * BufferList::getBuffer(int choice)
308 if (choice >= bstore.size()) return 0;
309 return bstore[choice];
313 void BufferList::updateInset(Inset * inset, bool mark_dirty)
315 for (BufferStorage::iterator it = bstore.begin();
316 it != bstore.end(); ++it) {
318 && (*it)->getUser()->text->UpdateInset(inset)) {
327 int BufferList::unlockInset(UpdatableInset * inset)
329 if (!inset) return 1;
330 for(BufferStorage::iterator it = bstore.begin();
331 it != bstore.end(); ++it) {
333 && (*it)->getUser()->the_locking_inset == inset) {
334 (*it)->getUser()->insetUnlock();
342 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
344 for(BufferStorage::iterator it = bstore.begin();
345 it != bstore.end(); ++it) {
346 if (!(*it)->isDepClean(mastertmpdir)) {
347 string writefile = mastertmpdir;
349 writefile += ChangeExtension((*it)->fileName(),
351 (*it)->makeLaTeXFile(writefile, mastertmpdir,
353 (*it)->markDepClean(mastertmpdir);
359 void BufferList::emergencyWriteAll()
361 for (BufferStorage::iterator it = bstore.begin();
362 it != bstore.end(); ++it) {
363 if (!(*it)->isLyxClean()) {
366 lyxerr <<_("lyx: Attempting to save"
369 << _(" as...") << endl;
371 for (int i = 0; i < 3 && !madeit; ++i) {
374 // We try to save three places:
375 // 1) Same place as document.
376 // 2) In HOME directory.
377 // 3) In "/tmp" directory.
379 s = (*it)->fileName();
381 s = AddName(GetEnvPath("HOME"),
384 // MakeAbsPath to prepend the current
385 // drive letter on OS/2
386 s = AddName(MakeAbsPath("/tmp/"),
391 lyxerr << " " << i + 1 << ") " << s << endl;
393 if ((*it)->writeFile(s, true)) {
394 (*it)->markLyxClean();
395 lyxerr << _(" Save seems successful. "
399 lyxerr << _(" Save failed! Trying...")
402 lyxerr << _(" Save failed! Bummer. "
412 Buffer * BufferList::readFile(string const & s, bool ronly)
414 Buffer * b = bstore.newBuffer(s, lyxrc, ronly);
417 string e = OnlyPath(s);
419 // File information about normal file
420 FileInfo fileInfo2(s);
422 // Check if emergency save file exists and is newer.
423 e += OnlyFilename(s) + ".emergency";
424 FileInfo fileInfoE(e);
426 bool use_emergency = false;
428 if (fileInfoE.exist() && fileInfo2.exist()) {
429 if (fileInfoE.getModificationTime()
430 > fileInfo2.getModificationTime()) {
431 if (AskQuestion(_("An emergency save of this document exists!"),
432 MakeDisplayPath(s, 50),
433 _("Try to load that instead?"))) {
435 // the file is not saved if we load the
438 use_emergency = true;
440 // Here, we should delete the emergency save
446 if (!use_emergency) {
447 // Now check if autosave file is newer.
449 a += OnlyFilename(s);
451 FileInfo fileInfoA(a);
452 if (fileInfoA.exist() && fileInfo2.exist()) {
453 if (fileInfoA.getModificationTime()
454 > fileInfo2.getModificationTime()) {
455 if (AskQuestion(_("Autosave file is newer."),
456 MakeDisplayPath(s, 50),
457 _("Load that one instead?"))) {
459 // the file is not saved if we load the
463 // Here, we should delete the autosave
469 // not sure if this is the correct place to begin LyXLex
472 if (b->readFile(lex))
481 bool BufferList::exists(string const & s)
483 for (BufferStorage::iterator it = bstore.begin();
484 it != bstore.end(); ++it) {
485 if ((*it)->fileName() == s)
492 Buffer * BufferList::getBuffer(string const & s)
494 for(BufferStorage::iterator it = bstore.begin();
495 it != bstore.end(); ++it) {
496 if ((*it)->fileName() == s)
503 Buffer * BufferList::newFile(string const & name, string tname)
505 /* get a free buffer */
506 Buffer * b = bstore.newBuffer(name, lyxrc);
508 // use defaults.lyx as a default template if it exists.
510 tname = LibFileSearch("templates", "defaults.lyx");
512 if (!tname.empty() && IsLyXFilename(tname)){
513 bool templateok = false;
517 if (b->readFile(lex)) {
522 WriteAlert(_("Error!"), _("Unable to open template"),
523 MakeDisplayPath(tname));
524 // no template, start with empty buffer
525 b->paragraph = new LyXParagraph;
528 else { // start with empty buffer
529 b->paragraph = new LyXParagraph;
533 b->setReadonly(false);
539 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
541 // make sure our path is absolute
542 string s = MakeAbsPath(filename);
544 // Is this done too early?
546 if (IsSGMLFilename(s)) {
548 if (fi.exist() && fi.readable()) {
549 if (!RunLinuxDoc(-1, s)) {
550 s = ChangeExtension (s, ".lyx", false);
551 } else { // sgml2lyx failed
552 WriteAlert(_("Error!"),
553 _("Could not convert file"), s);
557 // just change the extension and it will be
558 // handled like a regular lyx file that does
560 s = ChangeExtension(s, ".lyx", false);
564 // file already open?
566 if (AskQuestion(_("Document is already open:"),
567 MakeDisplayPath(s, 50),
568 _("Do you want to reload that document?"))) {
569 // Reload is accomplished by closing and then loading
570 if (!close(getBuffer(s))) {
573 // Fall through to new load. (Asger)
575 // Here, we pretend that we just loaded the
582 switch (IsFileWriteable(s)) {
584 current_view->owner()->getMiniBuffer()->
585 Set(_("File `")+MakeDisplayPath(s, 50)+
586 _("' is read-only."));
592 b->lyxvc.file_found_hook(s);
594 break; //fine- it's r/w
596 // Here we probably should run
597 if (LyXVC::file_not_found_hook(s)) {
598 // Ask if the file should be checked out for
599 // viewing/editing, if so: load it.
600 if (AskQuestion(_("Do you want to retrive file under version control?"))) {
601 // How can we know _how_ to do the checkout?
602 // With the current VC support it has to be,
603 // a RCS file since CVS do not have special ,v files.
605 return loadLyXFile(filename, tolastfiles);
608 if (AskQuestion(_("Cannot open specified file:"),
609 MakeDisplayPath(s, 50),
610 _("Create new document with this name?")))
612 // Find a free buffer
613 b = newFile(s, string());
618 if (b && tolastfiles)
619 lastfiles->newFile(b->fileName());