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
20 #include <sys/types.h>
22 #include "bufferlist.h"
24 #include "minibuffer.h"
25 #include "support/FileInfo.h"
26 #include "support/filetools.h"
27 #include "lyx_gui_misc.h"
28 #include "lastfiles.h"
31 #include "lyxscreen.h"
36 extern BufferView * current_view;
37 extern MiniBuffer * minibuffer;
38 extern void SmallUpdate(signed char);
39 extern void BeforeChange();
40 extern int RunLinuxDoc(int, string const &);
43 // Class BufferStorage
46 void BufferStorage::release(Buffer * buf)
48 for(Container::iterator it = container.begin();
49 it != container.end(); ++it) {
51 Buffer * tmpbuf = (*it);
60 Buffer * BufferStorage::newBuffer(string const & s,
64 Buffer * tmpbuf = new Buffer(s, lyxrc, ronly);
65 tmpbuf->params.useClassDefaults();
66 lyxerr.debug() << "Assigning to buffer "
67 << container.size() + 1 << endl;
68 container.push_back(tmpbuf);
77 BufferList::BufferList()
79 _state = BufferList::OK;
83 bool BufferList::empty()
85 return bstore.empty();
89 extern void MenuWrite(Buffer *);
91 bool BufferList::QwriteAll()
93 bool askMoreConfirmation = false;
95 for(BufferStorage::iterator it = bstore.begin();
96 it != bstore.end(); ++it) {
97 if (!(*it)->isLyxClean()) {
98 switch(AskConfirmation(_("Changes in document:"),
99 MakeDisplayPath((*it)->fileName(),
101 _("Save document?"))) {
106 askMoreConfirmation = true;
107 unsaved += MakeDisplayPath((*it)->fileName(), 50);
115 if (askMoreConfirmation &&
116 lyxrc->exit_confirmation &&
117 !AskQuestion(_("Some documents were not saved:"),
118 unsaved, _("Exit anyway?"))) {
126 // Should probably be moved to somewhere else: BufferView? LyXView?
127 bool BufferList::write(Buffer * buf, bool makeBackup)
129 minibuffer->Set(_("Saving document"),
130 MakeDisplayPath(buf->fileName()), "...");
132 // We don't need autosaves in the immediate future. (Asger)
133 buf->resetAutosaveTimers();
137 string s = buf->fileName() + '~';
138 // Rename is the wrong way of making a backup,
139 // this is the correct way.
140 /* truss cp fil fil2:
141 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
142 stat("LyXVC.lyx", 0xEFFFF688) = 0
143 open("LyXVC.lyx", O_RDONLY) = 3
144 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
145 fstat(4, 0xEFFFF508) = 0
146 fstat(3, 0xEFFFF508) = 0
147 read(3, " # T h i s f i l e w".., 8192) = 5579
148 write(4, " # T h i s f i l e w".., 5579) = 5579
149 read(3, 0xEFFFD4A0, 8192) = 0
152 chmod("LyXVC3.lyx", 0100644) = 0
153 lseek(0, 0, SEEK_CUR) = 46440
157 // Should proabaly have some more error checking here.
158 // Should be cleaned up in 0.13, at least a bit.
159 // Doing it this way, also makes the inodes stay the same.
160 // This is still not a very good solution, in particular we
161 // might loose the owner of the backup.
162 FileInfo finfo(buf->fileName());
164 mode_t fmode = finfo.getMode();
166 struct utimbuf *times =
167 (struct utimbuf*)new char[sizeof(struct utimbuf)];
168 times->actime = finfo.getAccessTime();
169 times->modtime = finfo.getModificationTime();
170 long blksize = finfo.getBlockSize();
171 lyxerr.debug() << "BlockSize: " << blksize << endl;
172 FilePtr fin(buf->fileName(), FilePtr::read);
173 FilePtr fout(s, FilePtr::truncate);
174 if (fin() && fout()) {
175 char * cbuf = new char[blksize+1];
179 c_read = fread(cbuf, 1, blksize, fin);
182 fwrite(cbuf, 1, c_read, fout);
186 chmod(s.c_str(), fmode);
188 if (utime(s.c_str(), times)) {
189 lyxerr << "utime error." << endl;
193 lyxerr << "LyX was not able to make backupcopy. Beware." << endl;
199 if (buf->writeFile(buf->fileName(), false)) {
202 minibuffer->Set(_("Document saved as"),
203 MakeDisplayPath(buf->fileName()));
205 // now delete the autosavefile
206 string a = OnlyPath(buf->fileName());
208 a += OnlyFilename(buf->fileName());
210 FileInfo fileinfo(a);
211 if (fileinfo.exist()) {
212 if (remove(a.c_str()) != 0) {
213 WriteFSAlert(_("Could not delete "
214 "auto-save file!"), a);
218 // Saving failed, so backup is not backup
220 string s = buf->fileName() + '~';
221 rename(s.c_str(), buf->fileName().c_str());
223 minibuffer->Set(_("Save failed!"));
231 void BufferList::closeAll()
233 _state = BufferList::CLOSING;
234 while (!bstore.empty()) {
235 close(bstore.front());
237 _state = BufferList::OK;
241 void BufferList::resize()
243 for(BufferStorage::iterator it = bstore.begin();
244 it != bstore.end(); ++it) {
250 bool BufferList::close(Buffer * buf)
254 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
256 switch(AskConfirmation(_("Changes in document:"),
257 MakeDisplayPath(buf->fileName(), 50),
258 _("Save document?"))){
261 lastfiles->newFile(buf->fileName());
279 void BufferList::makePup(int pup)
280 /* This should be changed to return a char const *const
281 in the same way as for lastfiles.[hC]
285 for(BufferStorage::iterator it = bstore.begin();
286 it != bstore.end(); ++it) {
287 string relbuf = MakeDisplayPath((*it)->fileName(), 30);
288 fl_addtopup(pup, relbuf.c_str());
291 if (ant == 0) fl_addtopup(pup, _("No Documents Open!%t"));
295 Buffer * BufferList::first()
297 if (bstore.empty()) return 0;
298 return bstore.front();
302 Buffer * BufferList::getBuffer(int choice)
304 if (choice >= bstore.size()) return 0;
305 return bstore[choice];
309 void BufferList::updateInset(Inset * inset, bool mark_dirty)
311 for (BufferStorage::iterator it = bstore.begin();
312 it != bstore.end(); ++it) {
314 if ((*it)->getUser() && (*it)->getUser()->text->UpdateInset(inset)) {
320 if ((*it)->text && (*it)->text->UpdateInset(inset)) {
330 int BufferList::unlockInset(UpdatableInset * inset)
332 if (!inset) return 1;
333 for(BufferStorage::iterator it = bstore.begin();
334 it != bstore.end(); ++it) {
335 if ((*it)->the_locking_inset == inset) {
336 (*it)->InsetUnlock();
344 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
346 for(BufferStorage::iterator it = bstore.begin();
347 it != bstore.end(); ++it) {
348 if (!(*it)->isDepClean(mastertmpdir)) {
349 string writefile = mastertmpdir;
351 writefile += ChangeExtension((*it)->fileName(),
353 (*it)->makeLaTeXFile(writefile, mastertmpdir,
355 (*it)->markDepClean(mastertmpdir);
362 void BufferList::emergencyWriteAll()
364 for (BufferStorage::iterator it = bstore.begin();
365 it != bstore.end(); ++it) {
366 if (!(*it)->isLyxClean()) {
369 lyxerr <<_("lyx: Attempting to save"
372 << _(" as...") << endl;
374 for (int i = 0; i < 3 && !madeit; ++i) {
377 // We try to save three places:
378 // 1) Same place as document.
379 // 2) In HOME directory.
380 // 3) In "/tmp" directory.
382 s = (*it)->fileName();
384 s = AddName(GetEnvPath("HOME"),
386 } else { // MakeAbsPath to prepend the current drive letter on OS/2
387 s = AddName(MakeAbsPath("/tmp/"),
392 lyxerr << " " << i + 1 << ") " << s << endl;
394 if ((*it)->writeFile(s, true)) {
395 (*it)->markLyxClean();
396 lyxerr << _(" Save seems successful. "
400 lyxerr << _(" Save failed! Trying...")
403 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
411 Buffer * BufferList::readFile(string const & s, bool ronly)
413 Buffer * b = bstore.newBuffer(s, lyxrc, ronly);
416 string e = OnlyPath(s);
418 // File information about normal file
419 FileInfo fileInfo2(s);
421 // Check if emergency save file exists and is newer.
422 e += OnlyFilename(s) + ".emergency";
423 FileInfo fileInfoE(e);
425 bool use_emergency = false;
427 if (fileInfoE.exist() && fileInfo2.exist()) {
428 if (fileInfoE.getModificationTime()
429 > fileInfo2.getModificationTime()) {
430 if (AskQuestion(_("An emergency save of this document exists!"),
431 MakeDisplayPath(s, 50),
432 _("Try to load that instead?"))) {
434 // the file is not saved if we load the
437 use_emergency = true;
439 // Here, we should delete the emergency save
445 if (!use_emergency) {
446 // Now check if autosave file is newer.
448 a += OnlyFilename(s);
450 FileInfo fileInfoA(a);
451 if (fileInfoA.exist() && fileInfo2.exist()) {
452 if (fileInfoA.getModificationTime()
453 > fileInfo2.getModificationTime()) {
454 if (AskQuestion(_("Autosave file is newer."),
455 MakeDisplayPath(s, 50),
456 _("Load that one instead?"))) {
458 // the file is not saved if we load the
462 // Here, we should delete the autosave
468 // not sure if this is the correct place to begin LyXLex
471 if (b->readFile(lex))
480 bool BufferList::exists(string const & s)
482 for (BufferStorage::iterator it = bstore.begin();
483 it != bstore.end(); ++it) {
484 if ((*it)->fileName() == s)
491 Buffer * BufferList::getBuffer(string const & s)
493 for(BufferStorage::iterator it = bstore.begin();
494 it != bstore.end(); ++it) {
495 if ((*it)->fileName() == s)
502 Buffer * BufferList::newFile(string const & name, string tname)
504 /* get a free buffer */
505 Buffer * b = bstore.newBuffer(name, lyxrc);
507 // use defaults.lyx as a default template if it exists.
509 tname = LibFileSearch("templates", "defaults.lyx");
511 if (!tname.empty() && IsLyXFilename(tname)){
512 bool templateok = false;
516 if (b->readFile(lex)) {
521 WriteAlert(_("Error!"), _("Unable to open template"),
522 MakeDisplayPath(tname));
523 // no template, start with empty buffer
524 b->paragraph = new LyXParagraph;
527 else { // start with empty buffer
528 b->paragraph = new LyXParagraph;
532 b->setReadonly(false);
538 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
540 // make sure our path is absolute
541 string s = MakeAbsPath(filename);
543 // Is this done too early?
545 if (IsSGMLFilename(s)) {
547 if (fi.exist() && fi.readable()) {
548 if (!RunLinuxDoc(-1, s)) {
549 s = ChangeExtension (s, ".lyx", false);
550 } else { // sgml2lyx failed
551 WriteAlert(_("Error!"),
552 _("Could not convert file"), s);
556 // just change the extension and it will be
557 // handled like a regular lyx file that does
559 s = ChangeExtension(s, ".lyx", false);
563 // file already open?
565 if (AskQuestion(_("Document is already open:"),
566 MakeDisplayPath(s, 50),
567 _("Do you want to reload that document?"))) {
568 // Reload is accomplished by closing and then loading
569 if (!close(getBuffer(s))) {
572 // Fall through to new load. (Asger)
574 // Here, we pretend that we just loaded the
581 switch (IsFileWriteable(s)) {
583 minibuffer->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 lyxerr << "Do you want to checkout?" << endl;
600 if (AskQuestion(_("Cannot open specified file:"),
601 MakeDisplayPath(s, 50),
602 _("Create new document with this name?")))
604 // Find a free buffer
605 b = newFile(s, string());
610 if (b && tolastfiles)
611 lastfiles->newFile(b->fileName());